java - if else 语句失败

标签 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 与 else 分开)。即使这是我第一次为这个程序选择坐标。我对编程真的很陌生,也许我犯了一个对其他人来说显而易见的菜鸟错误。

最佳答案

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

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

关于java - if else 语句失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29873508/

相关文章:

java - 用于截断字符串的正则表达式

java - 访问相机和存储图像 Marshmallow

java - 意外的顶级异常 : com. android.dex.DexException:多个 dex 文件定义

java - HQL hibernate 中的 "show databases"

java - 为什么 java 中的 collection.toArray() 不支持自动装箱

java - 使用 Hibernate Tools 5.1.0.Final 时出现 InspirationTargetException

c# - 如何使用 Quartz 调度程序维护作业历史记录

java - 从 Android Spinner 中获取枚举值

java - 在 Java db (Netbeans) 中创建外键

java - 摆脱枚举中的代码重复