如果else语句失败

时间:2015-04-26 03:50:37

标签: java

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是其他的一部分)。即使这是我第一次选择该程序的坐标。我对编程很陌生,也许我已经犯了一个对其他人来说非常明显的菜鸟错误。

1 个答案:

答案 0 :(得分:1)

删除嵌套的while循环。这会阻止h更改,

for ( h = 1 ; h <= userInput; h++) { 
    // while (h <= userInput) {
    // ...
    // }
}