[英]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.