Java 中的 while 循环不起作用

发布于 2024-12-06 20:48:42 字数 758 浏览 0 评论 0原文

好吧,问题是这样的:

我想输入一堆数字,一组随机的数字,比如 5 个数字。在该数字的末尾,我将添加第六个数字 0,它将作为循环结束的信号。

为了实现这个目标,这就是我要做的。

我运行我的程序,这是我的输入:

1 3 4 2 5 0

所以我希望程序打印

1 3 4 2 5“序列结束”

这是我的代码,据说会做这样的事情(所有这些都在 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;    
 }

但是,这些是我得到的结果:

https://i.sstatic.net/DUFhs.jpg

有趣的是,如果我没有 while 循环停止,我再次输入相同的数字列表 (1 3 4 2 5 0)它会输出 3 和 2,这是我的数字列表中的偶数 - 所有这些都让我更加烦恼。

有什么想法吗?

Alright, so the problem is this:

I want to enter a bunch of numbers, a random set, of say 5 numbers. At the end of that number, I'll add a sixth number, 0, which will be a signal for my loop to end.

To achieve this, this is what I'll do.

I run my program and this is my input:

1 3 4 2 5 0

So I want the program to print

1
3
4
2
5 "End of sequence"

Here's my code that will purportedly do such a thing (all of this is in main method):

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:

https://i.sstatic.net/DUFhs.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.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

红墙和绿瓦 2024-12-13 20:48:42

将所有块(包括 if 块)包裹在大括号中!

这:

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

并不像你想象的那样工作,因为它的行为是相反,

if (foo) {
  doSomething();
}

doSomethingElse();

all 块包裹在大括号中,这样就毫无疑问:

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

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();
}
眼角的笑意。 2024-12-13 20:48:42

这看起来像是一个学习练习,所以我会给你一些提示,而不是直接告诉你。

  1. 在您选择的 IDE 中,告诉它自动缩进您的代码。仔细查看 if 语句及其结果所在的位置。

  2. 仔细考虑 nextInt() 的作用以及调用它的位置和频率。

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. Look closely at where the if statement and its results fall.

  2. Think carefully about what nextInt() does and about where and how often you want to call it.

彡翼 2024-12-13 20:48:42

您在循环中调用 input.nextInt() 两次,仅打印其中之一。实际上,这就像跳过输入中的所有其他数字。更改代码以将下一个输入存储到变量中,然后检查该变量。我怀疑这不是您的实际代码,因为检查条件时没有将 notZero 设置为 false,它只会执行一次循环。 FWIW,notZero 是一个糟糕的变量名。它应该是语义的,而不是表明其期望值。如果输入值不是输入指示符结束,您还需要仅打印输入值。

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);
   } 
}

You are calling input.nextInt() twice in the loop, only printing one of them. 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. FWIW, notZero is a lousy variable name. 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);
   } 
}
裂开嘴轻声笑有多痛 2024-12-13 20:48:42

你的问题在这里:

 if (input.nextInt() == 0)

当你调用 nextInt() 两次时,它会删除两个数字。每次迭代只需调用一次。

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

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

  }

解决这个问题以及充满鳗鱼的气垫船指出的问题,你应该会很好。

Your problem is here:

 if (input.nextInt() == 0)

When you call nextInt() twice it removes two numbers. 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.

说好的呢 2024-12-13 20:48:42

每次循环中都会调用 nextInt() 两次。这意味着每次,您都会使用两个数字,对每个数字执行不同的操作 - 即,您打印的数字和与 0 比较的数字是两个不同的数字。您只需调用 nextInt() 一次并将结果存储在变量中,然后对其进行操作。

You're calling nextInt() twice each time through the loop. That means that each time around, you'll consume two numbers, doing different things with each one -- i.e., the one you print and the one you compare to 0 are two different numbers. You need to call nextInt() just once and store the result in a variable, then operate on it.

护你周全 2024-12-13 20:48:42

除了循环内的两次读取之外,我假设您希望在 if 语句之后有一个块

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

Besides the two reads inside your loop, I assume you want to have a block after your if-statement

if (input.nextInt() == 0) {
    System.out.println("End of sequence");
    notZero = false;    
}
极致的悲 2024-12-13 20:48:42

据我所知,notZero 的唯一目的是终止循环。如果您使用在循环条件下终止的循环,则不需要它。您也不需要在循环内打印“序列结束”,因为根据定义,它会在序列结束后打印。

如果我们直接测试条件,

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

您也可以说 for (int n = input.nextInt(); n != 0; n = input.nextInt()) 而不是 。它确保 n 仅在循环内可见,但这也意味着重复自己 - 并留下了以后有人可能使两个“初始化”和“增量”部分不同的可能性。

Far as i see, notZero's only purpose is to terminate the loop. 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文