简体   繁体   English

突破For Loop Java

[英]Breaking out of For Loop Java

I have this piece of code here: 我在这里有这段代码:

Scanner input = new Scanner(System.in);
int array[] = new int[10];

System.out.println("Enter the numbers now.");

for (int i = 0 ; i < array.length; i++ ) {        
    if (input.nextInt() == 999){        
        break;
    } else {
        array[i] = input.nextInt();
    }
}

I want to break out of the loop if the user enters 999 inside the array but so far no luck. 如果用户在数组内输入999,但到目前为止还算不上运气,我想打破循环。 I tried using break or return false but nothing works. 我尝试使用break或return false,但没有任何效果。 Does anyone have a solution? 有没有人有办法解决吗? Much thanks! 非常感谢!

You are using input.nextInt(); 您正在使用input.nextInt(); twice.That is reading from console in if and else . 两次。从ifelse控制台读取。

 for (int i = 0 ; i < array.length; i++ ) {
      int enteredNumber = input.nextInt();
      if (enteredNumber == 999){
          break;
      } else {
         array[i] = enteredNumber ;
      }
 }

You are reading twice inside your loop. 您正在循环中阅读两次。 So, if your if condition is falsy (user does not enter 999 ), then it will go into else block where you are reading a new input from user, which can possibly be 999 . 因此,如果您的if条件是虚假的(用户未输入999 ),则它将进入else块,在该块中您正在从用户读取新输入,可能是999

Change your loop to: 将循环更改为:

for (int i = 0 ; i < array.length; i++ ) {
    int read = input.nextInt();
    if (read == 999) {
        break;
    }
    array[i] = read;
}

Apart from that, you should also consider the case where user doesn't actually passes an integer, in which case, your code will blow. 除此之外,您还应该考虑用户实际上没有传递整数的情况,在这种情况下,您的代码会崩溃。 You can use Scanner#hasNextInt() method for that. 您可以为此使用Scanner#hasNextInt()方法。

for (int i = 0 ; i < array.length; i++ ) {
    while (!input.hasNextInt()) {
        System.out.println("You must pass an integer");
        input.next();  // Advance the scanner past the current line.
    }
    int read = input.nextInt();
    if (read == 999) {
        break;
    }
    array[i] = read;
}

Of course, that loop might run forever if user keeps on entering non-integer values. 当然,如果用户继续输入非整数值,则该循环可能永远运行。 To overcome that, you can give user a maximum number of attemps. 为了解决这个问题,您可以为用户提供最大的尝试次数。 That I'll leave up to you to handle. 那我留给你处理。 (HINT: You will need a counter that goes from 0 to max. On each loop iteration, reset it). (提示:您将需要一个从0到最大值的计数器。在每次循环迭代中,将其重置)。

The way you currently have it is that you're calling nextInt() multiple times within each iteration. 您目前拥有它的方式是,你调用nextInt()每次迭代中多次

That means you'll lose data. 这意味着您将丢失数据。 Let's say you first enter 7. That's picked up in the if statement as "not 999" so it moves onto the else clause where you ask the user for yet another number (you've lost the 7). 假设您首先输入7。在if语句中将其提取为“ not 999”,因此它移到else子句中,您在该子句中要求用户提供另一个数字(您丢失了7)。

In addition, you'll only break out of that loop if you enter 999 when it's executing the first call to nextInt() . 此外,如果在执行nextInt()第一次调用时输入999,则只会跳出该循环。 If you enter 999 when it's executing the second call, it will just store it and keep going. 如果您在执行第二个呼叫时输入999,它将存储它并继续进行。

Try this instead: 尝试以下方法:

for (int i = 0 ; i < array.length; i++ ) {
    int next = input.nextInt();
    if (next == 999)
        break;
    array[i] = next;
}

Try this thing in your for loop. 在您的for循环中尝试一下。

for (int i = 0 ; i < array.length; i++ ) { for(int i = 0; i <array.length; i ++){

int number = input.nextInt();

   if (number == 999){
        break;
    }
   System.out.println("aghsdgha" + number);

} }

This is the simpler and cleaner way to check the input number. 这是检查输入数字的更简便的方法。

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

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