简体   繁体   English

如何退出Java循环?

[英]How to exit Java loop?

If you could give me some help with an assignment I have for my Java class, I'd greatly appreciate it.如果您能为我的 Java 课程作业提供一些帮助,我将不胜感激。 The prompt for the question is:问题的提示是:

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

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从评论中编辑:您的分配方向错误,因此您将输入设置为等于 min/max 而不是将 min/max 设置为等于 input

Move the read of input inside the 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只会存在于循环中,这是它唯一使用/需要的地方.

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

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

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 ();如您所见,我添加in = keyboard.nextInt (); to enable getting more values from the user能够从用户那里获得更多价值

You are putting the values in same variable "in"您将值放在同一个变量“in”中

in=max;在 = 最大;

It should be other way应该是其他方式

max=in;最大值=输入;

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

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

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