如何退出Java循环?

时间:2013-03-28 20:55:58

标签: java loops break

如果你能为我的Java课程作业提供一些帮助,我会非常感激。问题的提示是:

编写程序以读取非负整数列表并显示最大整数,最小整数和所有整数的平均值。用户通过输入未用于查找最大值,最小值和平均值的负标记值来指示输入的结束。平均值应该是double类型的值,以便用小数部分计算。

我遇到的问题是,在运行时,循环没有完成,除非输入的第一个值是负数,在这种情况下它会返回:

输入的最大数量为:0 输入的最小数字为:0 输入的数字的平均值为:NaN

请帮忙!谢谢。 -SAM

代码:

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

7 个答案:

答案 0 :(得分:3)

您需要在循环内再次读取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;
    //}
}

从评论中编辑:你的作业方向错误,所以你设置的输入等于min / max而不是设置min / max等于输入

答案 1 :(得分:1)

将输入的读取移到循环中,然后打开负数:

while (true) {
    in = keyboard.nextInt();
    if (in < 0) break;
    // rest of loop
}

更好的appraoch将是使用for循环,它很好地捆绑了所有与循环相关的逻辑:

for (int in = keyboard.nextInt(); in >= 0; in = keyboard.nextInt()) {
    // your current loop code
}

分离迭代代码可以清楚地知道迭代代码是什么代码,并使循环代码完全专注于程序的任务,使得更容易阅读和理解

这也意味着您不需要声明int in,并且最好尽可能地减少一个变量的范围 - 在这种情况下in只存在于循环中,这是它唯一使用/需要的地方。

答案 2 :(得分:1)

您读取值的语句不在while循环中,因此它只读取第一个条目:

in = keyboard.nextInt ();
while (in>=0)
{

}

更改为:

in = keyboard.nextInt ();
while (in>=0)
{
   ... stuff ...
in = keyboard.nextInt ();
}

答案 3 :(得分:0)

您必须重新读取用户的输入。而不是:

in = keyboard.nextInt ();
while (in>=0) {
    if (in>max){
       in=max;
    }
    if (in<min){
        in=min;
    }

    sum += in;
    count++;
    if (in<0){
         break;
    }
}

使用:

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!
}

答案 4 :(得分:0)

将您的代码更改为

in = keyboard.nextInt ();
while (in>=0){
    if (in>max){
        in=max;
    }

    if (in<min){
        in=min;
    }

    sum += in;
    count++;
    in = keyboard.nextInt ();
}

如您所见,我已添加in = keyboard.nextInt ();以启用从用户那里获取更多值

答案 5 :(得分:0)

您将值放在“in”

中的相同变量中

在=最大值;

应该是其他方式

最大=在;

答案 6 :(得分:0)

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