java 如何退出Java循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15691981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to exit Java loop?
提问by Sleepy
If you could give me some help with an assignment I have for my Java class, I'd greatly appreciate it. The prompt for the question is:
如果您能帮助我完成 Java 课程的作业,我将不胜感激。问题的提示是:
Write a program to read a list of non-negative integers and to display the largest integer, the smallest integer, and the average of all the integers. The user indicates the end of the input by entering a negative sentinel value that is not used in finding the largest, smallest, and average values. The average should be a value of type double so that it is computed with a fractional part.
编写一个程序来读取非负整数列表并显示最大整数、最小整数和所有整数的平均值。用户通过输入一个不用于查找最大值、最小值和平均值的负标记值来指示输入的结束。平均值应该是 double 类型的值,以便用小数部分计算。
The problem I'm experiencing with my code is that when run, the loop does not finish unless the first value entered is negative, in which case it returns:
我的代码遇到的问题是,在运行时,除非输入的第一个值是负数,否则循环不会完成,在这种情况下它返回:
The maximum number entered was: 0 The minimum number entered was: 0 The average of the numbers entered was: NaN
输入的最大数字为:0 输入的最小数字为:0 输入数字的平均值为:NaN
Please help! Thanks. -Sam
请帮忙!谢谢。-山姆
code:
代码:
package blah;
import java.util.Scanner;
public class blahblah
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println ("Please enter a list of positive integers.");
System.out.println ("Please enter a negative integer when finished.");
int in = 0;
int max = 0;
int min = 0;
int sum = 0;
int count = 0;
in = keyboard.nextInt();
while (in>=0)
{
if (in > max) {
in = max;
}
if (in < min) {
in = min;
}
sum += in;
count++;
if (in < 0) {
break;
}
}
System.out.println("The maximum number entered was: " + max);
System.out.println("The minimum number entered was: " + min);
System.out.println("The average of the numbers entered was: " + (double)sum/count);
}
}
回答by Mike Clark
You need to read the nextInt again inside you loop:
您需要在循环中再次读取 nextInt :
while (in>=0)
{
if (in>max){
max=in;
}
if (in<min){
min=in;
}
sum += in;
count++;
in = keyboard.nextInt();
//Check not needed here, handled by while loop
//if (in<0){
// break;
//}
}
Edit from comment: your assignment was going the wrong direction, so you were setting input equal to min/max instead of setting min/max equal to input
从评论中编辑:您的分配方向错误,因此您将输入设置为等于最小/最大,而不是将最小/最大设置为等于输入
回答by Bohemian
Move the read of input insidethe loop, and break on negative:
在循环内移动输入的读取,并在负数处中断:
while (true) {
in = keyboard.nextInt();
if (in < 0) break;
// rest of loop
}
A better appraoch would be to use a for
loop, which nicely bundles up all the loop-related logic:
更好的方法是使用for
循环,它很好地捆绑了所有与循环相关的逻辑:
for (int in = keyboard.nextInt(); in >= 0; in = keyboard.nextInt()) {
// your current loop code
}
Separating out the iteration code makes it clear what code is the iteration code and leaves the loop code to be entirely dedicated to the program's task, making ing easier to read and understand
将迭代代码分离出来,可以清楚哪些代码是迭代代码,让循环代码完全专用于程序的任务,使 ing 更易于阅读和理解
This also means you don't need to declare int in
, and it is good practice to reduce the scope of one's variables as much as possible - in this case in
would only exists within the loop, which is the only place it's used/needed.
这也意味着您不需要声明int in
,并且尽可能减少一个变量的范围是一种很好的做法 - 在这种情况下in
,它只会存在于循环中,这是它唯一使用/需要的地方。
回答by OldProgrammer
You statement to read the values is not inside the while loop, so it is only reading the first entry:
您读取值的语句不在 while 循环内,因此它仅读取第一项:
in = keyboard.nextInt ();
while (in>=0)
{
}
Change to:
改成:
in = keyboard.nextInt ();
while (in>=0)
{
... stuff ...
in = keyboard.nextInt ();
}
回答by NullPointerException
You are putting the values in same variable "in"
您将值放在同一个变量“in”中
in=max;
在 = 最大;
It should be other way
应该是其他方式
max=in;
最大值=输入;
回答by Muhammad Talha
Scanner input =new Scanner(System.in);
double a= 0;
int i=0;
double sum=0;
double average=0;
double smallest=0;
double largest=0;
double range=0;
while(a>=0){
System.out.print("Enter the value : ");
a=input.nextDouble();
if(a<0){
break;
}
i++;
sum+=a;
average=sum/i;
if(smallest==0){
smallest=a;
}
if(smallest>a){
smallest=a;
}
if(largest<a){
largest=a;
}
range=largest-smallest;
}
System.out.print("\n The average of all the values is : "+average);
System.out.print("\n The smallest of the values is : "+smallest);
System.out.print("\n The largest of the values is : "+largest);
System.out.print("\n The range of the values is : "+range);
回答by tmwanik
Change your code to
将您的代码更改为
in = keyboard.nextInt ();
while (in>=0){
if (in>max){
in=max;
}
if (in<min){
in=min;
}
sum += in;
count++;
in = keyboard.nextInt ();
}
As you can see i have added in = keyboard.nextInt ();
to enable getting more values from the user
正如你所看到的,我添加in = keyboard.nextInt ();
了从用户那里获得更多价值的内容
回答by Mordechai
You have to reread input from user. Instead of:
您必须重新读取用户的输入。代替:
in = keyboard.nextInt ();
while (in>=0) {
if (in>max){
in=max;
}
if (in<min){
in=min;
}
sum += in;
count++;
if (in<0){
break;
}
}
Use:
利用:
in = keyboard.nextInt ();
while (in>=0) {
if (in>max){
in=max;
}
if (in<min){
in=min;
}
sum += in;
count++;
// removed if, since loop checks it.
in = keyboard.nextInt (); // read on!
}