for (h = 1; h <= userInput; h++) {
while (h <= userInput) {
System.out.println("\nPick a Y coordinate: (1 - " + row + ")");
try {
userInput2 = stdin.nextInt();
if (userInput2 > 0 && userInput2 <= row) {
} else {
System.out.println("Make sure you only choose a"
+ " coordinate between 1 and " + row);
}
} catch (Exception f) {
System.out.println("Make sure your only choosing a coordinate"
+ " between 1 and " + row);
stdin.next();
continue;
}
System.out.println("\nPick a X coordinate: (1 - " + column + ")");
try {
userInput3 = stdin.nextInt();
if (userInput3 > 0 && userInput3 <= column) {
} else {
System.out.println("Make sure you only choose a"
+ " coordinate between 1 and " + column);
}
} catch (Exception g) {
System.out.println("Make sure your only choosing a coordinate"
+ " between 1 and " + column);
stdin.next();
continue;
}
if (userGuesses[userInput2][userInput3] == false) {
userGuesses[userInput2][userInput3] = true;
}
if (h == userInput) {
break;
} else {
System.out.println("You've already chosen that coordinate coward!"
+ " Try again!");
continue;
}
}
}
由于某种原因,最后一个 if else
语句总是被打印出来(SoP 与 else 分开)。即使这是我第一次为这个程序选择坐标。我对编程真的很陌生,也许我犯了一个对其他人来说显而易见的菜鸟错误。
最佳答案
删除嵌套的 while
循环。它阻止 h
更改,
for ( h = 1 ; h <= userInput; h++) {
// while (h <= userInput) {
// ...
// }
}
关于java - if else 语句失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29873508/