Java 如何编写程序以获取值的最小值、最大值和平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21065379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a program to get minimum, maximum and average of values
提问by user2858657
public static void main(String[] args) {
int sum = 0;
int inputNum;
int counter;
float average;
double Max = 0;
double Min = 101;
Scanner NumScanner = new Scanner(System.in);
Scanner charScanner = new Scanner(System.in);
System.out.println("Enter the total number of exams you want a average");
counter = NumScanner.nextInt();
System.out.println("Please enter " + counter + " numbers:");
for(int i = 1; i<=counter ;i++){
inputNum = NumScanner.nextInt();
sum = sum + inputNum;
System.out.println();
if(inputNum > Max){
Max = inputNum;
}
if(inputNum < Min){
Min = inputNum;
}
if(inputNum > -1 && inputNum < 101){
sum = sum + inputNum;
}
else{
System.out.println("You entered a number that wasn't in the range of 0 to 100");
average = sum / counter;
}
}
}
}
Write a program using a loop that takes 10 values from a user representing exam grades (between 0 and 100) from the keyboard and outputs the minimum value, maximum value and average value of all the values entered. Your program should not accept values less than 0 or greater than 100. Problems with calculation of average and the program does not print out max and min values
使用循环编写一个程序,该程序从键盘上获取代表考试成绩(0 到 100 之间)的用户的 10 个值,并输出所有输入值的最小值、最大值和平均值。您的程序不应接受小于 0 或大于 100 的值。 计算平均值时出现问题,程序不打印出最大值和最小值
How can I do this?
我怎样才能做到这一点?
采纳答案by user2858657
//This code is how to print out the max and min values of a list of numbers from the above program//
//这段代码是如何从上面的程序中打印出一个数字列表的最大值和最小值//
// Print out of max and min exam grades//
System.out.println( "Max Exam Score = " + Max );
System.out.println( "Minimum Exam Score = " + Min `enter code here`);