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是其他的一部分)。即使这是我第一次选择该程序的坐标。我对编程很陌生,也许我已经犯了一个对其他人来说非常明显的菜鸟错误。
答案 0 :(得分:1)
删除嵌套的while
循环。这会阻止h
更改,
for ( h = 1 ; h <= userInput; h++) {
// while (h <= userInput) {
// ...
// }
}