首页 > 解决方案 > 为什么我的 while 循环只对某些嵌套的“if”语句有效,而对其他语句无效?

问题描述

while 循环应该询问用户他们想要选择哪个选项,并且在选项完成后它应该返回到 while 循环的开始。现在,它只适用于 second else if,没有别的。

while(flag){
        System.out.println("Select an option from the menu below:" + 
        "\n1: Display consecutive numbers in a right triangle" + //needs better explanation
        "\n2: Determine if a number is divisible by 3" + 
        "\n3: Determine the number of periods and spaces in a string" +
        "\n4: Display a right triangle of astrisks" + 
        "\n5: Exit"); //better explanation
        String option = input.nextLine();

        if(option.equals("1")){  //does not work
            System.out.println("Enter number of columns: ");
            int width = input.nextInt(); 
            while(width<=0 || width%1!=0){
                System.out.println("Invalid input! Please enter a positive width.");
                width = input.nextInt(); 
            }
            numOfColumns(width);
        }

        else if(option.equals("2")){ //does not work
            System.out.println("Enter number:");
            int num = input.nextInt();
            divisibleBy3(Math.abs(num));
        }

        else if(option.equals("3")){ //works
            System.out.println("Enter string:");
            String sentence = input.nextLine();
            System.out.println("Number of periods and spaces: " + periodsAndSpaces(sentence) + "\n");

        }
        
        else if(option.equals("4")){ //does not work
            System.out.println("Enter the width of the triangle:");
            int length = input.nextInt();
            rightTriangle(length, "");
        }

        else if(option.equals("5")){ //exits the while loop
            flag = false;
        }

        else{
            System.out.println("That is not a valid option!");
        }

    }

对于上面指出的 if/else if 语句,程序将执行给定的方法,然后显示菜单,然后是“这不是有效的输入!”,然后再次显示菜单,即使用户没有输入任何内容。

标签: java

解决方案


正如评论中提到的,您需要在使用input.nextInt();或任何其他不消耗整行的方法后消耗该行的其余部分,因为它只需要输入部分行,其余部分保留在扫描仪上。您可以通过input.nextLine();越过当前行来解决此问题,以便在 while 循环重新启动时为下一个输入做好准备,如下所示:

//Get a partial line input eg "nextInt()"
int someInput = input.nextInt();
//Consume the rest of the line
input.nextLine();
//Now process the input
...

这是使用上述技术的工作解决方案:

    if(option.equals("1")){  //does not work
        System.out.println("Enter number of columns: ");
        int width = input.nextInt(); 
        //Consume the rest of the line
        input.nextLine();
        //Now process the input
        while(width<=0 || width%1!=0){
            System.out.println("Invalid input! Please enter a positive width.");
            width = input.nextInt(); 
        }
        numOfColumns(width);
    }

    else if(option.equals("2")){ //does not work
        System.out.println("Enter number:");
        int num = input.nextInt();
        //Consume the rest of the line
        input.nextLine();
        //Now process the input
        divisibleBy3(Math.abs(num));
    }

    else if(option.equals("3")){ //works
        System.out.println("Enter string:");
        String sentence = input.nextLine();
        System.out.println("Number of periods and spaces: " + periodsAndSpaces(sentence) + "\n");

    }
    
    else if(option.equals("4")){ //does not work
        System.out.println("Enter the width of the triangle:");
        int length = input.nextInt();
        //Consume the rest of the line
        input.nextLine();
        //Now process the input
        rightTriangle(length, "");
    }

    else if(option.equals("5")){ //exits the while loop
        flag = false;
    }

    else{
        System.out.println("That is not a valid option!");
    }

这允许每个选项正常工作。


推荐阅读