java写一串代码创建一个学生类,姓名,年龄,语文分数,数学分数,英语分数,添加几条数据,再输出
时间: 2024-04-19 15:29:40 浏览: 85
好的,下面是使用Java语言创建学生类、添加数据并输出的示例代码:
```java
public class Student {
private String name;
private int age;
private int chineseScore;
private int mathScore;
private int englishScore;
public Student(String name, int age, int chineseScore, int mathScore, int englishScore) {
this.name = name;
this.age = age;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
}
// Getter and Setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getChineseScore() {
return chineseScore;
}
public void setChineseScore(int chineseScore) {
this.chineseScore = chineseScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
}
public class Main {
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("学生姓名:" + student1.getName() + ",年龄:" + student1.getAge() + ",语文分数:" + student1.getChineseScore() +
",数学分数:" + student1.getMathScore() + ",英语分数:" + student1.getEnglishScore());
System.out.println("学生姓名:" + student2.getName() + ",年龄:" + student2.getAge() + ",语文分数:" + student2.getChineseScore() +
",数学分数:" + student2.getMathScore() + ",英语分数:" + student2.getEnglishScore());
System.out.println("学生姓名:" + student3.getName() + ",年龄:" + student3.getAge() + ",语文分数:" + student3.getChineseScore() +
",数学分数:" + student3.getMathScore() + ",英语分数:" + student3.getEnglishScore());
}
}
```
运行以上代码将得到如下输出:
```
学生姓名:张三,年龄:18,语文分数:80,数学分数:90,英语分数:85
学生姓名:李四,年龄:17,语文分数:75,数学分数:95,英语分数:90
学生姓名:王五,年龄:16,语文分数:85,数学分数:80,英语分数:95
```
这样就使用Java语言创建了一个学生类,并添加了几条数据,并输出了学生的信息。
阅读全文