简体   繁体   中英

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. 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.

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

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.

Min and Max were now equal to the max and min of integer. now if any number is less than min and greater than max, min and max takes their position. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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