用java李红:数学98,语文90,物理87,化学85,英语90 张毅:数学90,语文85,物理98 使用可变长度参数方法输出学生姓名和成绩
时间: 2024-05-09 20:15:19 浏览: 136
可以使用以下代码实现:
```java
public class Student {
private String name;
private int[] scores;
public Student(String name, int... scores) {
this.name = name;
this.scores = scores;
}
public String getName() {
return name;
}
public int[] getScores() {
return scores;
}
}
public class Main {
public static void main(String[] args) {
Student lihong = new Student("李红", 98, 90, 87, 85, 90);
Student zhangyi = new Student("张毅", 90, 85, 98);
printScores(lihong);
printScores(zhangyi);
}
public static void printScores(Student student) {
System.out.print(student.getName() + "的成绩为:");
for (int score : student.getScores()) {
System.out.print(score + " ");
}
System.out.println();
}
}
```
输出结果为:
```
李红的成绩为:98 90 87 85 90
张毅的成绩为:90 85 98
```
阅读全文