简体   繁体   中英

how to calculate min, max, sum, and avg

import java.util.*;
public class Lab5_2{
    public static void main(String[]args){

        Scanner scan = new Scanner(System.in);
        int num=0;
        boolean choice=true;
        int min= Math.min(100, num);// need to fix the min to the avg, won't print out
        int max= Math.max(0, num);
        int sum=+ num;
        double avg= sum/(num+1);

        while(choice==true){
            if(num>=0 && num<=100){
                System.out.println("Please enter a number between 0 and 100 ");
                num= scan.nextInt();
                System.out.println("You entered: " + num);     
            }else if(num<0 || num>100){
                choice=false;
                System.out.println("Thank you ");
            }
        } 
    }
    /*
       System.out.println("Min = " + min);
       System.out.println("Max = " + max);
       System.out.println("Sum = " + sum);
       System.out.println("Avg = " + avg);*/
}

I know this is way easier then I am making it seem, but I can't for the life of me figure out how to get my code to calculate the min,max,sum, and avg. I need it so that I can input any number any amount of numbers and compute them not using arrays(since my class hasn't gotten there).

Thank you in advance

First, you should take the Scanner.read outside of the while loop, because you don't want numbers that are not within your range to be processed:

while (choice) {
    System.out.println("Please enter a number between 0 and 100 ");
    num = scan.nextInt();
    if (num >= 0 && num <= 100) {
        // accept number and update min, max, sum 
    } else { 
        choice = false; 
    }
}

Now, let us calculate those values we're after:

  • Calculating the sum is easy: Initially, our sum is 0 , and each accepted number is accumulated. So to calculate the sum, we simply add num , ie, sum = sum + num .

  • The minimum is always at least as big as the minimum from the previous iteration. We do not need to know the values from any of the other iterations, so no arrays (or other collections) are required to store them. We simply check whether num is less than the previous minimum, and replace the minimum, if this is true, ie, if (num < min) min = num; . There is also a shorthand notation for that: min = num < min ? num : min; min = num < min ? num : min;

  • Computing the maximum is identical to computing the minimum (just in reverse). I'm sure you can figure out the solution.

  • The average cannot be updated in each itearation. But we can compute it at the end. What is the average? It is the sum divided by the number of the summands. So you need to count how many times you read a number. You can do this by storing an additional variable that you increment in each iteration.

With this information, you should be able to write the code. Note that the correct initialization of your variables before entering the loop is crucial. Carefully think about that. Also, there is a corner case that you need to consider: What happens, if the user does not input a single number? What should your values be in that case? Do not forget that division by zero is undefined!

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