【发布时间】:2018-04-26 23:34:17
【问题描述】:
我需要帮助显示用户输入的平均数、最小数和最高数,但我只能显示平均数和最大数。这里还有另一个这样的问题,但没有完全解决,只给出最小的数字。如果有除 Math.min 和 Math.max 之外的其他方式,将不胜感激。
import java.text.DecimalFormat;
import java.util.Scanner;
public class ProblemSet3_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat dec = new DecimalFormat();
int snum = 0;
int height = 0;
int add = 0;
double avg = 0;
int largest = 0;
int smallest = 0;
System.out.print("How much students are in your class?" + '\n');
snum = input.nextInt();
for (int i = 1; i <= snum; i++) {
System.out.print("How tall is student #" + i + "?" + '\n');
height = input.nextInt();
add += height;
avg = add / i;
if (height > largest) {
largest = height;
}
}
System.out.print("The average height is " + avg + ", while the tallest is " + largest + " , and the shortest is " + smallest + ".");
}
}
【问题讨论】:
-
你用谷歌搜索过这个吗?
-
@GBlodgett 是的,我什么都试过了,所以我决定自己问这个问题
-
你已经得到了最大的,你如何以同样的方式得到最小的?
-
您期望的数字是否小于 0?
标签: java