32 lines
1.0 KiB
Java
32 lines
1.0 KiB
Java
package chapter2;
|
|
|
|
public class ClassScoreAnalyzer {
|
|
public static void main(String[] args) {
|
|
// 1. 创建并初始化数组
|
|
int[] testScores = {85, 92, 78, 95, 88};
|
|
|
|
// 2. 使用循环打印所有学生的成绩
|
|
for (int i = 0; i < testScores.length; i++) {
|
|
System.out.println("学生" + (i + 1) + "的成绩:" + testScores[i]);
|
|
}
|
|
|
|
// 4. 调用 findHighestScore 方法并打印结果
|
|
int highestScore = findHighestScore(testScores);
|
|
System.out.println("班级最高分是:" + highestScore);
|
|
}
|
|
|
|
// 3. 编写 findHighestScore 方法
|
|
public static int findHighestScore(int[] scores) {
|
|
if (scores == null || scores.length == 0) {
|
|
return -1; // 返回-1表示数组为空或null
|
|
}
|
|
|
|
int max = scores[0]; // 假设第一个元素是最大的
|
|
for (int i = 1; i < scores.length; i++) {
|
|
if (scores[i] > max) {
|
|
max = scores[i];
|
|
}
|
|
}
|
|
return max;
|
|
}
|
|
} |