简体   繁体   English

如何手动退出while循环(java)

[英]How to manually exit a while loop (java)

This code will take user input from the console, and go to one of two places depending on if the input is an Integer or not. 该代码将从控制台获取用户输入,并根据输入是否为Integer转到两个位置之一。 The exception is caught and then dealt with by reading it again to get it out of the stream. 该例外被捕获,然后处理再次阅读它把它弄出来的流。 (not sure if Scanner counts as a stream, but I know that without the input.next() it will never leave that section). (不确定Scanner是否算作流,但是我知道没有input.next()它将永远不会离开该部分)。

So what I want to know is how can I exit the loop if I get to the MismatchException with a certain input. 因此,我想知道的是,如果使用特定输入到达MismatchException,如何退出循环。 I've tested the if(bad_input == anything) and it turns out I can never actually get there. 我测试过,如果(bad_input ==任何东西),它原来我从来没有真正到达那里。 I tried some print statements in that block that never executed. 我在该块中尝试了一些从未执行过的打印语句。 Would appreciate any help. 将不胜感激。

Solved 解决了

Not sure how to close the question, but this has been solved. 不知道如何关闭的问题,但是这已经解决了。 I was incorrectly using == to check two strings. 我错误地使用==检查两个字符串。 The correct way is to use bad_input.equals("x"). 正确的方法是使用bad_input.equals(“X”)。

public static void main(String[] args){
  //read input from console
  Scanner input = new Scanner(System.in);

  boolean loop = true;

  while(loop){
    //inputting "1 2 d 1" will evaluate each term separately and output at once
    try{

      //choice = console input only in INTEGER
      int choice = input.nextInt();
      System.out.println("Input was " + choice);

    } catch (InputMismatchException ex){
      //here if input != INTEGER

      String bad_input = input.next();
      System.out.println("Bad Input: " + bad_input);

      //need to figure out how to exit loop!!
      if(bad_input == "x"){
           loop = false;
           return;
      }
      continue;
    }
  }
}

In java String comparison is using equals function "==" might not work 在java中,字符串比较使用的是equals函数“ ==”可能不起作用

Change 更改

if (bad_input == "x") {
    loop = false;
    return;
}

To

if (bad_input.equals("x")) {
    loop = false;
    return;
}

Try to use break. 尝试使用break。

if(condition)
    break;

break; will exit the loop immediately. 将立即退出循环。 What you're doing should also work though. 不过,您正在执行的操作也应该起作用。 I'm not sure you need the if statement though, the whole catch block will be executed if and only if the input isn't an integer. 我不确定您是否需要if语句,仅当输入不是整数时,才会执行整个catch块。

Well I think you need to use 好吧,我认为你需要使用

 if(bad_input.equals("x")){
       loop = false;
       return;
  }

== checks for same reference ==检查相同的参考

.equals checks for same values .equals检查相同的值

http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/ http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/

May be you should use equalsIgnoreCase method. 可能是您应该使用equalsIgnoreCase方法。

if (bad_input.equalsIgnoreCase("x")) {

And to exit the loop at any value other then integer, you can simply use break . 而要以除整数之外的任何其他值退出循环,只需使用break即可

catch (InputMismatchException ex) {
                // here if input != INTEGER

                String bad_input = input.next();
                System.out.println("Bad Input: " + bad_input);

                break;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM