简体   繁体   English

Java中的While循环不起作用

[英]While-loop in Java not working

Alright, so the problem is this: 好了,所以问题是这样的:

I want to enter a bunch of numbers, a random set, of say 5 numbers. 我想输入一堆数字,一个随机数,例如5个数字。 At the end of that number, I'll add a sixth number, 0, which will be a signal for my loop to end. 在该数字的末尾,我将添加第六个数字0,这将是循环结束的信号。

To achieve this, this is what I'll do. 为此,我将要做。

I run my program and this is my input: 我运行我的程序,这是我的输入:

1 3 4 2 5 0 1 3 4 2 5 0

So I want the program to print 所以我要打印程序

1 3 4 2 5 "End of sequence" 1 3 4 2 5“序列结束”

Here's my code that will purportedly do such a thing (all of this is in main method): 这是我的代码,据说可以做到这一点(所有这些都在main方法中):

Scanner input = new Scanner(System.in);
System.out.println("Alright, what?");
boolean notZero = true;
while (notZero)  
 {
   System.out.println(input.nextInt());
   if (input.nextInt() == 0)
       System.out.println("End of sequence");
       notZero = false;    
 }

These, however, are the results I get: 这些是我得到的结果:

http://i.imgur.com/4Th43.jpg http://i.imgur.com/4Th43.jpg

Interestingly, if I don't have the while-loop stop, and I input the same number list again (1 3 4 2 5 0) it'll output 3 and 2, which are the even numbers in my number list - and all this vexes me further. 有趣的是,如果我没有while循环停止,然后再次输入相同的数字列表(1 3 4 2 5 0),它将输出3和2,这是我的数字列表中的偶数-以及所有这进一步困扰了我。

Any ideas? 有任何想法吗?

Wrap all your blocks including if blocks in curly braces! 将所有块包装起来,包括如果将块装在花括号中!

This: 这个:

if (foo) 
  doSomething();
  doSomethingElse();

is not working as you think it is as it's behaving as 不能像您认为的那样工作

if (foo) {
  doSomething();
}

doSomethingElse();

Instead wrap all blocks in curly braces so that there is no doubt: 而是将所有块用大括号括起来,以便毫无疑问:

if (foo) {    
  doSomething();
  doSomethingElse();
}

This looks like a learning exercise, so I'll give you a few hints rather than tell you outright. 这看起来像是一项学习练习,所以我会给您一些提示,而不是直接告诉您。

  1. In your IDE of choice, tell it to auto-indent your code. 在您选择的IDE中,告诉它自动缩进您的代码。 Look closely at where the if statement and its results fall. 仔细查看if语句及其结果的位置。

  2. Think carefully about what nextInt() does and about where and how often you want to call it. 请仔细考虑nextInt()的功能,以及在何处以及多久调用一次。

You are calling input.nextInt() twice in the loop, only printing one of them. 您在循环中两次调用input.nextInt(),仅打印其中之一。 In effect, that is like skipping every other number in the input. 实际上,这就像跳过输入中的所有其他数字。 Change your code to store the next input into a variable, then check the variable. 更改您的代码以将下一个输入存储到变量中,然后检查该变量。 I suspect this isn't your actual code as without setting notZero to false as a result of checking the condition, it will only execute the loop once. 我怀疑这不是您的实际代码,因为没有通过检查条件将notZero设置为false ,它只会执行一次循环。 FWIW, notZero is a lousy variable name. FWIW, notZero是一个糟糕的变量名。 It should be semantic, not indicate its expected value. 它应该是语义的,而不是指示其期望值。 You'll also want to only print the input value if it isn't the end of input indicator. 您还希望仅在输入值不是输入指示符的结尾时打印输入值。

Scanner input = new Scanner(System.in);

System.out.println("Alright, what?");

boolean endOfInput = false;

while (!endOfInput) {
   int next = input.nextInt();

   if (next == 0) {
      System.out.println("End of sequence");
      endOfInput = true;
   }  
   else {
      System.out.println(next);
   } 
}

Your problem is here: 您的问题在这里:

 if (input.nextInt() == 0)

When you call nextInt() twice it removes two numbers. 当您调用nextInt()两次时,它将删除两个数字。 You need to only call it once per iteration. 您只需要在每个迭代中调用一次即可。

 while (notZero)  
 {
    int num = input.nextInt();
    System.out.println(num);

    if (num == 0) {
        System.out.println("End of sequence");
        notZero = false;    
    }

  }

Fix that along with the problem that Hovercraft Full of Eels pointed out and you should be good. 解决此问题以及气垫船充满鳗鱼指出的问题,你应该很好。

You're calling nextInt() twice each time through the loop. 您在整个循环中每次都要调用nextInt()两次。 That means that each time around, you'll consume two numbers, doing different things with each one -- ie, the one you print and the one you compare to 0 are two different numbers. 这意味着您每次都会消耗两个数字,每个数字都会做不同的事情-即,打印的数字和与0比较的数字是两个不同的数字。 You need to call nextInt() just once and store the result in a variable, then operate on it. 您只需要调用一次nextInt()并将结果存储在变量中, 然后对其进行操作。

Besides the two reads inside your loop, I assume you want to have a block after your if-statement 除了循环中的两次读取外,我假设您还想在if语句后添加一个块

if (input.nextInt() == 0) {
    System.out.println("End of sequence");
    notZero = false;    
}

Far as i see, notZero 's only purpose is to terminate the loop. 据我notZeronotZero的唯一目的是终止循环。 You don't need it if you use a loop that terminates on the loop condition. 如果使用以循环条件终止的循环,则不需要它。 You also don't need to print the "End of sequence" inside the loop, since by definition, it prints once the sequence is ended. 您也不需要在循环内打印“序列结束”,因为根据定义,它会在序列结束时打印。

If we test for the condition directly instead, 如果我们直接测试条件,

int n;
while ((n = input.nextInt()) != 0)
{
    System.out.println(n);
}
System.out.println("End of sequence");

You could also say for (int n = input.nextInt(); n != 0; n = input.nextInt()) instead of the while . 您也可以说for (int n = input.nextInt(); n != 0; n = input.nextInt())而不是while It ensures that n is only visible within the loop, but it also means repeating yourself -- and leaving open the possibility that someone could make the two 'initialization' and 'increment' sections different later. 它确保n仅在循环中可见,但这也意味着重复自己-并可能使某人稍后使两个“初始化”和“增量”部分不同。

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

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