Java 无限循环;不明白怎么办

Java 无限循环;不明白怎么办,java,loops,while-loop,Java,Loops,While Loop,在下面的代码中,当我输入除整数值以外的任何内容时,代码不会再次要求我输入,只会无限循环字符串输出。一点帮助 int choice = 0; while(choice == 0) { try { System.out.println("Start by typing the choice number from above and hitting enter: "); choice = inpu

在下面的代码中,当我输入除整数值以外的任何内容时,代码不会再次要求我输入,只会无限循环字符串输出。一点帮助

    int choice = 0;
    while(choice == 0)
    {
        try
        {
            System.out.println("Start by typing the choice number from above and hitting enter: ");
            choice = input.nextInt();
        }
        catch(Exception e)
        {

        }

        if ((choice == 1) || (choice == 2) || (choice == 3))
        {
            break;
        }
        else
        {
            System.out.println("Invalid choice number. Please carefully type correct option.");
            choice = 0;
        }
    }
在choice=input.nextInt; 选择值始终为0。在那之后很快打印选择

另外,对于非整数值,添加一个条件以从循环中断。

当使用nextInt且下一个输入不是int时,它将抛出异常但不使用数据,即下一个调用将立即返回,因为数据仍然存在


您可以通过使用类似[^0-9]*的模式调用skip方法来解决此问题,以跳过所有无效输入。然后像aaa3这样的输入就可以工作了。如果您想忽略所有内容,请使用.*作为模式。

当您输入非整数时,它将不会被使用。你需要扫描过去。例如,可以通过向catch块添加input.nextLine语句来实现。这将使用无效输入,并允许您的程序读取新数据

这将解决您的问题:

catch(Exception e)
{
    input.nextLine(); // Consume the invalid line
    System.out.println("Invalid choice number. Please carefully type correct option.");
}
您也可以将该行读取为字符串,并尝试使用Scanner.nextLine和Integer.parseInt将其解析为数字,但我更喜欢使用nextInt处理整数。在我看来,这使代码的目的更加明确。

您是否使用Scannersystem.in;从导入java.util.Scanner;包裹 尝试添加input.nextLine;在catch中清除值以停止无限循环

public static void main(String[] args) {

int choice = 0;
Scanner input = new Scanner(System.in);

while(choice == 0)
{
    try
    {
        System.out.println("Start by typing the choice number from above and hitting enter: ");
        choice = input.nextInt();
    }
    catch(Exception e)
    {
        input.nextLine();
        System.out.println("Invalid choice number. Please carefully type correct option.");
    }

    if ((choice == 1) || (choice == 2) || (choice == 3))
    {
        break;
    }
    else
    {
        System.out.println("Invalid choice number. Please carefully type correct option.");
        choice = 0;
    }
 }
}

问题是您没有使用流中剩余的数据。我用以下代码解决了这个问题,尽管您希望在程序中使用代码之前更好地记录代码:

int choice = 0;
while(choice == 0)
{
    try
    {
        System.out.print("Start by typing the choice number from above and hitting enter: ");
        choice = input.nextInt();
    }
    catch(Exception e)
    {
        input.next();
        System.out.println("Invalid choice number. Please carefully type correct option.");
    }

    if ((choice == 1) || (choice == 2) || (choice == 3))
    {
        break;
    }
    choice = 0;
}

您可以按如下方式简化和减少代码:

    int choice;
    System.out.println("Start by typing the choice number from above and hitting enter: ");
    while(true)
    {
        try {
            choice = input.nextInt();
            if ((choice == 1) || (choice == 2) || (choice == 3))
                break;
        } catch(InputMismatchException e) {  // handle only the specific exception
            input.nextLine();                // clear the input
        }
    System.out.println("Invalid choice number. Please carefully type correct option.");
    }

我得到了它。我声明了一个字符串变量响应,它接受input.next的值。然后我把它转换成了整数。无论如何,谢谢!哦那有什么问题?这对我来说很好。。。只是需要一个额外的休息…额外的休息?我不知道你的意思。问题是非整数输入的无限循环。哦,我明白了,我误解了。你必须使用nextLine,否则你会得到另一个像第一个一样的异常。我知道我的意思,只是编写了错误的函数,这不是很有帮助。我现在改了。谢谢你指出这一点。