Java 中的 while 循环不起作用
好吧,问题是这样的:
我想输入一堆数字,一组随机的数字,比如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将所有块(包括 if 块)包裹在大括号中!
这:
并不像你想象的那样工作,因为它的行为是相反,
将 all 块包裹在大括号中,这样就毫无疑问:
Wrap all your blocks including if blocks in curly braces!
This:
is not working as you think it is as it's behaving as
Instead wrap all blocks in curly braces so that there is no doubt:
这看起来像是一个学习练习,所以我会给你一些提示,而不是直接告诉你。
在您选择的 IDE 中,告诉它自动缩进您的代码。仔细查看
if
语句及其结果所在的位置。仔细考虑 nextInt() 的作用以及调用它的位置和频率。
This looks like a learning exercise, so I'll give you a few hints rather than tell you outright.
In your IDE of choice, tell it to auto-indent your code. Look closely at where the
if
statement and its results fall.Think carefully about what nextInt() does and about where and how often you want to call it.
您在循环中调用 input.nextInt() 两次,仅打印其中之一。实际上,这就像跳过输入中的所有其他数字。更改代码以将下一个输入存储到变量中,然后检查该变量。我怀疑这不是您的实际代码,因为检查条件时没有将
notZero
设置为false
,它只会执行一次循环。 FWIW,notZero 是一个糟糕的变量名。它应该是语义的,而不是表明其期望值。如果输入值不是输入指示符结束,您还需要仅打印输入值。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
tofalse
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.你的问题在这里:
当你调用 nextInt() 两次时,它会删除两个数字。每次迭代只需调用一次。
解决这个问题以及充满鳗鱼的气垫船指出的问题,你应该会很好。
Your problem is here:
When you call nextInt() twice it removes two numbers. You need to only call it once per iteration.
Fix that along with the problem that Hovercraft Full of Eels pointed out and you should be good.
每次循环中都会调用
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 callnextInt()
just once and store the result in a variable, then operate on it.除了循环内的两次读取之外,我假设您希望在 if 语句之后有一个块
Besides the two reads inside your loop, I assume you want to have a block after your if-statement
据我所知,notZero 的唯一目的是终止循环。如果您使用在循环条件下终止的循环,则不需要它。您也不需要在循环内打印“序列结束”,因为根据定义,它会在序列结束后打印。
如果我们直接测试条件,
您也可以说
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,
You could also say
for (int n = input.nextInt(); n != 0; n = input.nextInt())
instead of thewhile
. It ensures thatn
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.