编写java程序,输入学生的个数,在输入学生各自的成绩,最后求出分数中最高的分数
时间: 2024-04-12 07:31:43 浏览: 18
你可以使用循环和条件语句来实现这个功能。下面是一个示例程序,用于输入学生的个数和各自的成绩,并求出最高的分数:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生的个数:");
int studentCount = scanner.nextInt();
int maxScore = Integer.MIN_VALUE; // 初始化最高分为最小整数值
for (int i = 1; i <= studentCount; i++) {
System.out.print("请输入第 " + i + " 个学生的成绩:");
int score = scanner.nextInt();
if (score > maxScore) {
maxScore = score;
}
}
System.out.println("最高分数为:" + maxScore);
}
}
```
以上代码首先要求用户输入学生的个数。然后,使用循环逐个询问每个学生的成绩,并将当前成绩与最高分进行比较。如果当前成绩大于最高分,则将最高分更新为当前成绩。循环结束后,输出最高分数。
请注意,上述示例程序假设用户输入的是有效的整数。如果用户输入了非整数的值,程序将抛出 `InputMismatchException` 异常。你可以根据需要对输入进行验证或添加异常处理来提高程序的鲁棒性。