Java 如何退出此循环?

Java 如何退出此循环?,java,arrays,for-loop,while-loop,Java,Arrays,For Loop,While Loop,我的程序是为了在多维数组中搜索用户输入的状态。然后,它向州政府提供鸟和花。但是,我不知道需要将语句放在哪里才能退出循环。它将继续在数组中搜索输入,并像我希望的那样反复询问问题。但我尝试了几种不同的方法,当我输入none时,无法让它退出循环 Scanner userInput = new Scanner(System.in); while (true) { System.out.print("Enter a State or None to exit: "); String sta

我的程序是为了在多维数组中搜索用户输入的状态。然后,它向州政府提供鸟和花。但是,我不知道需要将语句放在哪里才能退出循环。它将继续在数组中搜索输入,并像我希望的那样反复询问问题。但我尝试了几种不同的方法,当我输入none时,无法让它退出循环

Scanner userInput = new Scanner(System.in);
while (true) {
    System.out.print("Enter a State or None to exit: ");
    String stateName = userInput.next();

    for (int i = 0; i < stateInformation.length; i++) {
        if (stateInformation[i][0].equalsIgnoreCase(stateName)) {
            System.out.println(stateInformation[i][0] + ":\n " +
                "The State bird is the " + stateInformation[i][1] + "\n " +
                "The State Flower is the " + stateInformation[i][2]);
            break;
        }
    }
}
Scanner userInput=新扫描仪(System.in);
while(true){
System.out.print(“输入状态或无以退出:”);
字符串stateName=userInput.next();
for(int i=0;i
我假设您想退出while循环。在for循环之前,您应该检查什么是用户输入,如果没有,则使用break。第二个选项是检查while参数中的输入是否为none。

我假设您想退出while循环。在for循环之前,您应该检查什么是用户输入,如果没有,则使用break。第二个选项是检查while的参数中的输入是否为none。

假设您想要中断for循环,则可以使用函数,然后使用return,如下所示:

私有对象搜索数据(…)
{
用于(t型:S2型){
如果(某些条件){
//做点什么然后打破。。。
返回对象;
}
}

}
假设您想要打破for循环,那么使用函数然后使用return就可以了,如下所示:

私有对象搜索数据(…)
{
用于(t型:S2型){
如果(某些条件){
//做点什么然后打破。。。
返回对象;
}
}

}
您的程序提示用户输入
None
退出。因此,添加一个条件来处理该行为:

String stateName = userInput.nextLine();
if (stateName.equalsIgnoringCase("none")) {
  break;
}
这将终止
while(true)
循环


如评论中所述,通常最好使用捕获用户输入。

您的程序提示用户输入
None
退出。因此,添加一个条件来处理该行为:

String stateName = userInput.nextLine();
if (stateName.equalsIgnoringCase("none")) {
  break;
}
这将终止
while(true)
循环


正如评论中所讨论的,通常最好使用它来捕获用户输入。

这就是您如何在您选择的特定语句上使用
break

myLabel:
while (true)
{
    while (true)
    {
        break myLabel; // This will break out of the outer loop that has been labeled.
    }
}

以下是您如何在您选择的特定语句中使用
break

myLabel:
while (true)
{
    while (true)
    {
        break myLabel; // This will break out of the outer loop that has been labeled.
    }
}

我想您应该检查userInput是否为“None”,然后像这样中断while循环:

while (true) {
    System.out.print("Enter a State or None to exit: ");
    String stateName = userInput.next();
    if ("None".equalsIgnoreCase(stateName)) break; // Added to break the while loop

    for (int i = 0; i < stateInformation.length; i++) {
        if (stateInformation[i][0].equalsIgnoreCase(stateName)) {
            System.out.println(stateInformation[i][0] + ":\n " +
                "The State bird is the " + stateInformation[i][1] + "\n " +
                "The State Flower is the " + stateInformation[i][2]);
            break;
        }
    }
}
while(true){
System.out.print(“输入状态或无以退出:”);
字符串stateName=userInput.next();
if(“None”.equalsIgnoreCase(stateName))break;//添加以中断while循环
for(int i=0;i
我想您应该检查userInput是否为“None”,然后像这样中断while循环:

while (true) {
    System.out.print("Enter a State or None to exit: ");
    String stateName = userInput.next();
    if ("None".equalsIgnoreCase(stateName)) break; // Added to break the while loop

    for (int i = 0; i < stateInformation.length; i++) {
        if (stateInformation[i][0].equalsIgnoreCase(stateName)) {
            System.out.println(stateInformation[i][0] + ":\n " +
                "The State bird is the " + stateInformation[i][1] + "\n " +
                "The State Flower is the " + stateInformation[i][2]);
            break;
        }
    }
}
while(true){
System.out.print(“输入状态或无以退出:”);
字符串stateName=userInput.next();
if(“None”.equalsIgnoreCase(stateName))break;//添加以中断while循环
for(int i=0;i
当收到输入“none”时,while()或for()我正试图中断while循环,您正在尝试中断哪些循环@pmcevoy12给了我我想要的东西。然而,现在我遇到了这样一个问题:当我输入一个2字的状态时,它会将我带回提示符,而没有任何状态输出。我猜我的循环没有考虑空格之类的东西,但我不确定挂起在哪里。要考虑空格,可以使用Scanner类的nextLine()方法,而不是next()方法。当输入为“none”时,你试图打破while循环的while()或for()收到了@pmcevoy12给了我我想要的东西。然而,现在我遇到了这样一个问题:当我输入一个2字的状态时,它会将我带回提示符,而没有任何状态输出。我猜我的循环没有考虑空格之类的东西,但我不确定挂起在哪里。要考虑空格,可以使用Scanner类的方法nextLine()而不是方法next()。谢谢!!这正是我需要的。我很接近,我只是把它放在了错误的区域,通常是在for循环之后,一直得到错误的结果。我没有想过把if语句放在输入之后,但是现在它工作得很好。谢谢!!这正是我需要的。我很接近,我只是把它放在了错误的区域,通常是在for循环之后,一直得到错误的结果。我没有考虑将if语句放在输入之后,但现在它工作得很好。我两个都试过了,因为你的语法不同,但我想它的意思是一样的。不,我注意到,当一个2字的字符出现时,程序会直接返回提示