如何计算最小值,最大值,总和和平均值 [英] how to calculate min, max, sum, and avg

查看:326
本文介绍了如何计算最小值,最大值,总和和平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

提前谢谢

推荐答案

首先,您应该将Scanner.read放在while循环之外,因为您不希望处理不在您范围内的数字: /p>

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:

  • 计算sum很容易:最初,我们的总和是0,并且每个接受的数字都会累加.因此,要计算总和,我们只需添加num,即sum = sum + num.

  • 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, i.e., sum = sum + num.

最小值始终至少与上一次迭代中的最小值一样大.我们不需要知道任何其他迭代的值,因此不需要数组(或其他集合)来存储它们.我们只需检查num是否小于先前的最小值,然后替换最小值(如果为true),即if (num < min) min = num;.还有一个简写形式:min = num < min ? num : min;

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, i.e., if (num < min) min = num;. There is also a shorthand notation for that: 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!

这篇关于如何计算最小值,最大值,总和和平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆