嗨,我是一名新手编码员,我整夜都在努力解决这个问题。 我正在开发一个刽子手游戏,用户必须输入他们想要猜测的字母以及他们想要检查的空格数。这个词当然被破折号隐藏了。当用户在正确的空间中得到正确的字母时,我应该用正确猜测的字母和空格打印出隐藏的单词。例如
单词:循环
隐藏:-----
您想猜什么字母: Ø
您想检查哪些空间: 1 2
隐藏:-oo--
我知道字符串是不可变的,所以我必须创建一个新字符串并将其与子字符串和用户输入的字母连接起来,但位置发生了变化,所以我必须重新排序每次连接它的方式是否正确?
并且我不允许使用数组、StringBuilder 或 StringBuffer
我希望我解释得足够清楚
这是我到目前为止所做的
Scanner keyboard = new Scanner(System.in);
while (true) {
System.out.println("Enter your difficulty: Easy (e), Intermediate (i), or Hard (h)");
String diff = keyboard.next();
diff = diff.substring(0, 1);
String guess = "";
String newGuess = "";
String newWord = "loops";//RandomWord.newWord();
int y = 0;
int count = 0;
for (int i = 0; i < newWord.length(); i++) {
newWord.charAt(i);
guess = newWord.replaceAll("[^#]", "-");
}
if ((diff.equalsIgnoreCase("e")) || (diff.equalsIgnoreCase("i")) || (diff.equalsIgnoreCase("h"))) {
System.out.println("The secret word is:" + " " + newWord);
System.out.println("The word is:" + " " + guess);
System.out.println("Please enter the letter you want to guess");
String letterInput = keyboard.next();
System.out.println("Please enter the spaces you want to check (seperated by spaces)");
String spaces = keyboard.nextLine();
spaces = keyboard.nextLine();
System.out.println(Character.getNumericValue(spaces.trim().charAt(count)));
for (int i = 0; i < spaces.trim().length(); i++) {
int x = Character.getNumericValue(spaces.trim().charAt(i));
if (x == 0) {
y = x - 1;
y = 0;
if ((letterInput.equalsIgnoreCase(newWord.substring(x, x + 1)))) {
newGuess = guess.substring(0, y) + guess.substring(y, x) + letterInput + guess.substring(x + 1);
System.out.println(newGuess);
}
}
else if ((letterInput.equalsIgnoreCase(newWord.substring(x, x + 1)))) {
newGuess = guess.substring(0, x) + guess.substring(x, x + 1) + letterInput + guess.substring(x + 1);
System.out.println(newGuess);
}
}
}
}
最佳答案
这里唯一的问题是扫描仪在玩弄你:
扫描仪的默认分隔符是空白字符
,因此当您输入包含空格的选项时,扫描仪仅读取第一个字符。
您的 Substring 方法也存在一些问题,我对您的代码进行了一些调整:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
keyboard.useDelimiter("\\n"); \\---------- this is what tells scanner to use new line char as delimiter
while (true) {
System.out.println("Enter your difficulty: Easy (e), Intermediate (i), or Hard (h)");
String diff = keyboard.next();
String guess = "";
String newGuess = "";
String newWord = "loops";//RandomWord.newWord();
int y = 0;
int count = 0;
for (int i = 0; i < newWord.length(); i++) {
guess = newWord.replaceAll("[^#]", "-");
}
if ((diff.equalsIgnoreCase("e")) || (diff.equalsIgnoreCase("i")) || (diff.equalsIgnoreCase("h"))) {
System.out.println("The secret word is:" + " " + newWord);
System.out.println("The word is:" + " " + guess);
System.out.println("Please enter the letter you want to guess");
String letterInput = keyboard.next();
System.out.println("Please enter the spaces you want to check (seperated by spaces)");
String spaces = keyboard.next();
for (String s : spaces.split("\\s")) {
int x = Integer.valueOf(s);
if (newWord.charAt(x) == letterInput.charAt(0)) {
System.out.println("Guess is correct for position " + x);
guess = guess.substring(0, x) + letterInput + guess.substring(x + 1, guess.length());
System.out.println(guess);
}
}
}
}
}
希望这有帮助!你就快到了
祝你好运!
关于java - 如何在位置改变时替换字符串的特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42871532/