Java编写一个程序,读入数目确定的考试分数,并且判断有多少个分数高于或等于平均分,有多少个分数低于平均分。输入一个负数标志输入结束,假设最高分为100。
时间: 2024-02-26 12:58:52 浏览: 64
好的,以下是您所需的Java程序:
```java
import java.util.Scanner;
public class ExamScores {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int count = 0;
int sum = 0;
int score = 0;
int highScore = 100;
System.out.print("Enter scores (-1 to end): ");
while (score >= 0) {
score = input.nextInt();
if (score >= 0 && score <= highScore) {
sum += score;
count++;
}
}
if (count > 0) {
double average = (double) sum / count;
int aboveAverage = 0;
int belowAverage = 0;
input.close();
input = new Scanner(System.in);
System.out.print("Enter scores again (-1 to end): ");
while (score >= 0) {
score = input.nextInt();
if (score >= 0 && score <= highScore) {
if (score >= average) {
aboveAverage++;
} else {
belowAverage++;
}
}
}
System.out.println("Average score is " + average);
System.out.println("Number of scores above or equal to the average is " + aboveAverage);
System.out.println("Number of scores below the average is " + belowAverage);
} else {
System.out.println("No valid scores entered.");
}
}
}
```
这个程序使用了两个 while 循环。第一个循环用于读入分数,计算总分和分数个数,以便计算平均分数。第二个循环用于再次读入分数,并计算高于或等于平均分数的个数和低于平均分数的个数。
注意,当输入负数时,程序将退出第一个循环并开始第二个循环。如果没有有效的分数输入,则程序将输出一条相应的消息。
阅读全文