public class Student{ private String name;private int age;private int chinesescore;private int mathsscore;private int englishscore; public void Student(String name, int age, int chinesescore, int mathsscore, int englishscore){ this.name=name;this.age=age;this.chinesescore=chinesescore;this.mathsscore=mathsscore;this.englishscore=englishscore; } public static void main(String[] args) { Student student1 = new Student("张三", 18, 80, 90, 85); Student student2 = new Student("李四", 17, 75, 95, 90); Student student3 = new Student("王五", 16, 85, 80, 95); System.out.println(); } } 怎么写才能输出张三李四王五的成绩
时间: 2024-04-14 22:29:08 浏览: 13
要输出张三、李四和王五的成绩,你可以在`Student`类中添加一个公共方法,用于打印学生的成绩。可以按照以下方式修改你的代码:
```java
public class Student {
private String name;
private int age;
private int chinesescore;
private int mathsscore;
private int englishscore;
public Student(String name, int age, int chinesescore, int mathsscore, int englishscore) {
this.name = name;
this.age = age;
this.chinesescore = chinesescore;
this.mathsscore = mathsscore;
this.englishscore = englishscore;
}
public void printScores() {
System.out.println("姓名: " + name);
System.out.println("语文成绩: " + chinesescore);
System.out.println("数学成绩: " + mathsscore);
System.out.println("英语成绩: " + englishscore);
System.out.println();
}
public static void main(String[] args) {
Student student1 = new Student("张三", 18, 80, 90, 85);
Student student2 = new Student("李四", 17, 75, 95, 90);
Student student3 = new Student("王五", 16, 85, 80, 95);
student1.printScores();
student2.printScores();
student3.printScores();
}
}
```
这样,运行`main`方法时,会依次输出每个学生的姓名和成绩。