Java while循环不执行else语句并重复循环

Java while循环不执行else语句并重复循环,java,while-loop,Java,While Loop,在未输入正确选择时进行编译时,不会显示重新输入或重复循环 int counterA = 0; while (counterA < 0) { if (conversionSelection.equalsIgnoreCase("binary")) counterA++; if (conversionSelection.equalsIgnoreCase("octal")) counterA++; else System.ou

在未输入正确选择时进行编译时,不会显示重新输入或重复循环

int counterA = 0;
while (counterA < 0) {
    if (conversionSelection.equalsIgnoreCase("binary"))
            counterA++;
    if (conversionSelection.equalsIgnoreCase("octal"))
            counterA++;
    else System.out.println("Error. Please enter weither to convert the Hex to Octal or Binary:");
        conversionSelection = keyboard.nextLine();
    }

循环根本不应该执行,因为条件从一开始就失败了


还有一个悬而未决的else声明。因此,如果希望else正确执行,那么第二个if语句需要有一个else-if。虽然当前可能不会产生错误,但防止挂起“else's”是一项重要技能。

您的代码永远不会进入While循环,因为counterA=0,While循环的条件是<0。您希望counterA小于零,或在其集合中包含counterA值的条件,例如,当counterA时,此循环将永远不会启动,因为0不小于0。

谢谢。。。我最初编写代码是为了得到else-if语句,但一直像else一样编写,不知道为什么它不起作用。