简体   繁体   English

如何计算最小值,最大值,总和和平均值

[英]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: 首先,您应该将Scanner.read放在while循环之外,因为您不希望处理不在您范围内的数字:

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. 计算sum很容易:最初,我们的总和为0 ,并且每个接受的数字都会累加。 So to calculate the sum, we simply add num , ie, sum = sum + num . 因此,要计算总和,我们只需添加num ,即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; 我们简单地检查num是否小于先前的最小值,如果为真,则替换最小值,即if (num < min) min = num; . There is also a shorthand notation for that: min = num < min ? num : min; 还有一种简写形式: 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! 不要忘记除以零是不确定的!

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

相关问题 如何在Java集合上执行Groupby,Sum,Avg,Min,Max? - How to do Groupby , Sum, Avg ,Min, Max on java collections? 如何检索COUNT()SUM()AVG()MIN()MAX()语句的结果集? - How to retrieve the resultset of COUNT() SUM() AVG() MIN() MAX() statements? 我们如何在java8中的DoubleSummaryStatistics对象中自定义count,avg,sum,min和max的顺序 - How can we customize order of count,avg,sum,min and max in DoubleSummaryStatistics object in java8 Java 2数组最小最大平均值? - Java 2 array min max avg? 在ArrayList中查找最小值,最大值,平均值 - Finding min, max, avg in ArrayList 如何计算地图的最小/最大键? - How to calculate min/max keys of map? 如何从(双)ArrayList 中找到最小值、最大值和平均值以打开 GUI(图形)? - How do I find the min, max and avg values from a (double) ArrayList to open a GUI (graph)? Java:如何使用表达式语言方法聚合(min,max,avg)集合元素属性? - Java: How to aggregate (min,max,avg) of collection element attributes with an expression language approach? Java:如何在支持最小,最大,平均,每组最后一种聚合的列表上进行聚合 - Java : How to do aggregation over a list supporting min, max, avg, last kind of aggregations in each group 如何根据输入计算一组计算的平均值和最大值/最小值? - How to calculate the average and max/min of an array of calculations from input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM