如果声明不起作用,请跳过其他地方

Nad*_*dim -1 java if-statement

我的"if"语句似乎不起作用,无论我输入什么似乎总是默认为我的"else"语句.例如,如果我输入整数"1"或"2",它们具有与它们相关联的"if"语句,则它仍然默认为"else".任何帮助,将不胜感激 :)

String number = JOptionPane.showInputDialog("Would you like a custom loop count or an infinite? 1.  Custom   2. Infinite"); //test choice
n = Integer.parseInt(number);
while (n < 0 || n > 2) {
    if (n == 1) {


        String number2 = JOptionPane.showInputDialog("How many times would you like to loop?");
        integer = Integer.parseInt(number2);
        while (integer < 0) {
            while (x < integer) {
                g.drawString("hi", 200, y);

                x += 1;
                y = y + 40; //test
            }//end while
        }
    }//end if 
    else if (n == 2) {
        while (1 == 1);
    }//end if   
    else;
}
g.drawString("Please pick a valid choice", 200, 200);
Run Code Online (Sandbox Code Playgroud)

ass*_*ias 7

如果阻止之后

while (n<0 || n>2)
Run Code Online (Sandbox Code Playgroud)

执行,你知道它n是<0或> 2.因此它不能等于1或2并且else执行该块,它恰好是空的,因为它后跟一个;.

这没有提到你的代码中发现的一些奇怪的陈述;-)

ps:我已经编辑了你的帖子,为你的代码添加了适当的缩进,并包括{}.现在看起来可能更清晰了.