【发布时间】:2015-04-09 18:17:29
【问题描述】:
所以我有课堂作业,我必须让用户在数组中输入值,获取平均值并输出高于或等于平均值的分数数,以及低于平均值的分数数。
但是,我只计算了平均部分。
这是我目前所拥有的:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of your array: ");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter the test scores");
for(int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
int sum = 0;
for(int b=0; b < a.length; b++) {
sum = sum + a[b];
}
double average = ((double)sum) / a.length;
System.out.println("The average is"+" " + average);
}
我想在输出高于或等于平均分数的分数和低于平均分数的分数的部分获得帮助。
【问题讨论】:
-
添加一个简单的布尔表达式。如果
scores > average... -
你可以在第二个类似的循环中使用
if (a[b] >= average)
标签: java