Java 为什么我的菜单不能正常工作?扫描仪不能正常工作
当我在菜单中选择2时,这部分也有问题。它正在工作,但它只是 如果我想猜测要猜测的单词,允许我输入字符而不是单词。我想要 只要我的机会数不等于零,就会显示菜单Java 为什么我的菜单不能正常工作?扫描仪不能正常工作,java,arrays,java.util.scanner,Java,Arrays,Java.util.scanner,当我在菜单中选择2时,这部分也有问题。它正在工作,但它只是 如果我想猜测要猜测的单词,允许我输入字符而不是单词。我想要 只要我的机会数不等于零,就会显示菜单 if(num == 1) { System.out.println("Please write your guessed word"); String guessWord = userInput.nextLine(); checkTheWord(gues
if(num == 1) {
System.out.println("Please write your guessed word");
String guessWord = userInput.nextLine();
checkTheWord(guessWord, word);
}
if(num==2){
ArrayList containerForChars=新的ArrayList();
while(剩余猜测>0){
System.out.println(“请键入一封信!”);
char typedLetter=userInput.next().charAt(0);
if(containerForChars.contains(typedLetter)){
System.out.println(“你已经试过那封信了”);
继续;
}
集装箱装卸工添加(typedLetter);
if(word.contains(typedLetter+“”)){
对于(int y=0;y
我在试着检查猜出的单词是否与待猜单词相同
猜测,但此代码不起作用。当我在菜单中选择1时,它
显示了println“请写下你猜到的单词”,但它没有
请允许我打字。我不知道我做错了什么
按以下步骤来解决此问题:
if(num == 2) {
ArrayList<Character> containerForChars = new ArrayList<Character>();
while (remainingGuess > 0) {
System.out.println("Please type a letter! ");
char typedLetter = userInput.next().charAt(0);
if (containerForChars.contains(typedLetter)) {
System.out.println("You have already tried that letter");
continue;
}
containerForChars.add(typedLetter);
if (word.contains(typedLetter + "")) {
for (int y = 0; y < word.length(); y++) {
if (word.charAt(y) == typedLetter) {
yourWord[y] = typedLetter;
}
}
} else {
remainingGuess--;
checkThenumOfGuesses(remainingGuess, word);
}
if (word.equals(String.valueOf(yourWord))) {
System.out.println(yourWord);
System.out.println("Congratulations you guessed the Word!");
break;
}
if (remainingGuess != 0) {
System.out.print(yourWord);
System.out.println(" tries remaining = " + remainingGuess);
}
}
}
}
public static void checkTheWord(String yourGuessedWord, String word) {
if(yourGuessedWord.equals(word)) {
System.out.println("Congratulations! You have guessed the secret word!");
}
}
public static void checkThenumOfGuesses(int remainingGuess, String word) {
if (remainingGuess == 0) {
System.out.println("You Lose! R.I.P." +
"\n ________" +
"\n | |" +
"\n | Ö" +
"\n | /|\\"+
"\n | / \\" +
"\n | " +
"\n/|\\ ");
System.out.println();
System.out.println("The secret word is " + word);
}
else if (remainingGuess == 1) {
System.out.println(" ________" +
"\n | |" +
"\n |" +
"\n |" +
"\n |" +
"\n |" +
"\n/|\\");
} else if (remainingGuess == 2) {
System.out.println(" ________"
+ "\n |" +
"\n |" +
"\n |" +
"\n |" +
"\n |" +
"\n/|\\");
} else if (remainingGuess == 3) {
System.out.println(" |" +
"\n |" +
"\n |" +
"\n |" +
"\n |" +
"\n |" +
"\n/|\\");
} else if (remainingGuess == 4) {
System.out.println("/|\\");
}
}
}
当我在菜单中选择2时,这部分也有问题。它是
工作,但它只允许我输入一个字符,而不是一个字,如果我
想猜被猜的单词
发生这种情况是因为以下几行:
System.out.println("Please write your guessed word");
userInput = new Scanner(System.in);
String guessWord = userInput.nextLine();
由于字符(0)
,此行只能接受输入的一个字符(第一个字符)。如果你想让它接受一个单词,你需要把它写成
char typedLetter = userInput.next().charAt(0);
for(int y=0;y
现在,当我将for循环更改为String而不是char@Millicent-不知怎的,我错过了你的评论。在寻找其他内容时,我刚刚看到了此页面。我不确定此解决方案是否适用于你,或者你是否实施了不同的解决方案。如果你实施了不同的解决方案,如果y你也有同样的想法。在任何一种情况下,你都可以通过接受对你有用的答案来帮助社区。接受的答案可以帮助未来的访问者自信地使用解决方案。查看以了解如何做到这一点。
char typedLetter = userInput.next().charAt(0);
String str = userInput.next();