简体   繁体   English

如何使用do-while循环获得五个数字的总和,平均值,最小值和最大值-java

[英]how to get the sum, average, minimum and maximum of five numbers-java using do-while loop

I'm trying to get the sum, average, minimum and maximum of five numbers but somehow I get this output. 我试图获得五个数字的总和,平均值,最小值和最大值,但不知怎的,我得到了这个输出。 I'm trying to re-code it all over again but it is still the same. 我试图重新编码它,但它仍然是相同的。 Can you help me check this guys... Here's my code: 你能帮我检查一下吗......这是我的代码:

import java.util.*;

public class Kleine {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double average;
        int count = 0, sum = 0, num, min = 0, max = 0;

        System.out.println("Please enter the number of numbers you wish to evaluate:");

        do {
            num = scan.nextInt();
            sum += num;
            count++;
        } while (count < 5);

        average = sum / 5;

        {
            if (num > max) {
                max = num;
            }

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

        System.out.println("Your average is: " + average);
        System.out.println("The sum is: " + sum);    

        System.out.println("Your maximum number is: " + max);
        System.out.println("Your minimum number is: " + min);
    }
}

Here's the output: 这是输出:

Please enter the number of numbers you wish to evaluate:
1
10
5
-3
6
Your average is3.0
The sum is:19
Your maximum number is 6
Your minimum number is 0
BUILD SUCCESSFUL (total time: 19 seconds)

The minimum and maximum numbers goes somewhere... a little advice please... 最小和最大数字在哪里...请一点意见...

The best way to handle the min/max values is to keep track of them as your read in each value: 处理最小/最大值的最佳方法是将它们作为每个值中的读数跟踪:

int sum = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

for (int i=0; i < 5; ++i) {
    num = scan.nextInt();
    if (num > max) max = num;
    if (num < min) min = num;
    sum += num;
}

double average = sum / 5.0d;

I seed the max and min values with the smallest/largest integer values, respectively. 我分别用最小/最大整数值对max和min值进行种子设定。 This lets us capture the actual min and max values as they are read in. Also, I think that a basic for loop works better here than a do while loop. 这让我们获得实际的最小值和最大值,因为他们在阅读。另外,我认为,一个基本for循环在这里工作比一个do while循环。

Note that I compute the average using a double type, because it may not be a pure integer value (even in your sample data the average is not an integer). 请注意,我使用double类型计算平均值,因为它可能不是纯整数值(即使在样本数据中,平均值也不是整数)。

Use 采用

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

And your 和你的

{
          if(num>max)
               max=num;
            if(num<min)
               min=num;
     }

needs to be inside the do-while loop, or else it runs only for the last value of number entered. 需要在do-while循环中,否则它只运行输入的最后一个值。

Min and Max were now equal to the max and min of integer. Min和Max现在等于整数的最大值和最小值。 now if any number is less than min and greater than max, min and max takes their position. 现在,如果任何数字小于min且大于max,则min和max取其位置。 The average and the sum functionality was great. 平均值和总和功能很棒。 The problem in your code was that it was getting the max and min after the loop for input has executed. 您的代码中的问题是在执行输入循环后获得最大值和最小值。 The flow was wrong. 流程是错误的。

import java.util.*;
        public class Kleine {
        public static void main(String[] args) {
            Scanner scan=new Scanner(System.in);
            double average;
        int  count=0, sum=0, num=0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;   
        System.out.println("Please enter the number of numbers you wish to evaluate:");
        do{
        if(num>max) max=num;
        if(num<min) min=num;
        num=scan.nextInt();
        sum+=num;
        count++;
        }while(count<5);
         average = sum/5;
        System.out.println("Your average is"+average);
        System.out.println("The sum is:"+sum);    
        System.out.printf("Your maximum number is %d\n",max);
        System.out.printf("Your minimum number is %d\n",min);
        }

    }

For a start you can use Math.min & Math.max . 首先,您可以使用Math.minMath.max The average is sum / count. 平均值是总和/计数。

An example getting a min number without a loop would be: 获取没有循环的最小数字的示例是:

long min = Integer.MAX_VALUE;
min = Math.min(min, 9);
min = Math.min(min, 4);
min = Math.min(min, 6);
// min = 4

Do something similar for max. 最大限度地做类似的事情。

You'd also be better off starting with a list or array of numbers. 从列表或数组开始你也会更好。 Get the output right, then add more complicated things like user input. 获得正确的输出,然后添加更复杂的内容,如用户输入。

You can do it this way without defining number of integers to read: 您可以这样做而无需定义要读取的整数:

import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println("Next number?");
            numbers.add(in.nextInt());
            IntSummaryStatistics summaryStatistics = numbers.stream()
                    .mapToInt(Integer::valueOf)
                    .summaryStatistics();
            System.out.println(String.format("Max: %d, Min: %d, Average: %s, Sum: %d", summaryStatistics.getMax(), summaryStatistics.getMin(), summaryStatistics.getAverage(), summaryStatistics.getSum()));
        }
    }
}

If I just change the existing code, the logic should be like below: 如果我只是更改现有代码,逻辑应如下所示:

do{
    num=scan.nextInt();
    sum+=num;
      if(count==0){
      min=num;
      max=num;}
      if(num>max)
               max=num;
            if(num<min)
               min=num;

    count++;
    }while(count<5);
     average = sum/5;

The issue was that your min-max condition was outside the loop and you were initializing min and max by 0. You should set min/max to your first input number. 问题是你的最小 - 最大条件是在循环之外并且你正在将min和max初始化为0.你应该将min / max设置为你的第一个输入数字。

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

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