【问题标题】:How do I exit this loop?如何退出这个循环?
【发布时间】:2016-10-16 20:03:01
【问题描述】:

我的程序旨在搜索多维数组以查找用户输入的状态。然后它供应州鸟和花卉。但是我不知道我需要在哪里放置语句以退出循环。它将继续在数组中搜索输入,并像我想要的那样一遍又一遍地问问题。但是我尝试了几种不同的方法,当我输入 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;
        }
    }
}

【问题讨论】:

  • 你试图跳出哪个循环,while() 或 for()
  • 当收到输入“none”时,我试图跳出 while 循环。 @pmcevoy12 给了我我想要的东西。但是,现在我遇到了一个问题,即当我进入 2 字状态时,它会将我带回到提示符,而没有任何状态输出。我猜我的循环没有考虑空格或其他东西,但我不确定挂断在哪里。
  • 为了考虑空格,您可以使用方法 nextLine() 而不是 Scanner 类的方法 next()。

标签: java arrays for-loop while-loop


【解决方案1】:

这是在您选择的特定语句上使用break 的方式:

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

【讨论】:

    【解决方案2】:

    我假设你想退出 while 循环。您应该在 for 循环之前检查用户输入的内容,如果没有,请使用 break。第二种选择是检查 while 参数中的输入是否为空。

    【讨论】:

      【解决方案3】:

      假设你想跳出for循环,使用函数然后使用return就可以了,如下图:

      private Object searchData(...) 
      {
              for (Type t : types2) {
                  if (some condition) {
                      // Do something and break...
                      return object;
                  }
              }
      }

      【讨论】:

        【解决方案4】:

        我认为您想检查 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;
                }
            }
        }
        

        【讨论】:

        • 谢谢!!这正是我所需要的。我很接近我只是把它放在错误的区域,通常在 for 循环之后并不断得到错误的结果。我没有考虑将 if 语句放在输入之后,但现在它可以完美运行。
        【解决方案5】:

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

        String stateName = userInput.nextLine();
        if (stateName.equalsIgnoringCase("none")) {
          break;
        }
        

        这将终止您的 while (true) 循环。

        正如 cmets 中所讨论的,通常最好使用 Scanner.nextLine() 来捕获用户输入。

        【讨论】:

        • 是的,这就像@pmcevoy12 推荐一样。我都试过了,因为你的语法不同,但我想这意味着同样的事情。不,我注意到,当输入 2 个单词状态时,程序会直接返回到提示。我猜是因为我可能没有考虑到空间,但我也不知道该怎么做。有什么建议吗?
        • userInput.next() 更改为userInput.nextLine()。userInput 变量是Scanner。它使用next() 作为空格分隔的字符串,使用nextLine() 读取直到换行。
        • "None".equals(stateName)stateName.equals("None") 是等价的,只要 stateName 不是 null。将文字 ("None") 放在首位是一种常见的“技巧”,用于处理可能为空值的编写不佳的代码。更标准的literal-last 格式通常被认为更易于阅读,但如果变量为null,则会触发NullPointerException。由于Scanner.next() 不返回nulls,因此无需使用文字优先技巧。
        • 正如@pmcevoy12 所说,Scanner.next() 读取单个标记(粗略地说,一个单词),而Scanner.nextLine() 读取整行。处理用户输入时,您通常希望一次读取一行(因此为.nextLine()),因为“Enter”键通常用于“提交”输入。
        猜你喜欢
        • 2014-11-28
        • 1970-01-01
        • 2010-12-18
        • 2017-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-04
        相关资源
        最近更新 更多