java定义一个学生类,有学号、姓名、性别、身份证号、成绩等信息,可以显示学生的信息、设置学生的成绩及成绩等级(90分以上为优秀,80-90分为良好,70-80分为中等,60-70分为及格,其余情况不及格)、设置学生的性别等功能。创建两个学生对象测试该类 要求:通过键盘输入姓名等,对成员变量进行封装,在get和set方法中对成绩进行检验,如果在0~100分以外,则弹出对话框报错;对性别进行检验,只能输入男女;对身份证号码进行检验,只能输入18位。
时间: 2023-05-26 10:04:22 浏览: 61
```
import javax.swing.JOptionPane;
public class Student {
private String id; // 学号
private String name; // 姓名
private String gender; // 性别
private String IDNumber; // 身份证号
private int score; // 成绩
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
if (!"男".equals(gender) && !"女".equals(gender)) {
JOptionPane.showMessageDialog(null, "性别输入有误,请重新输入!");
return;
}
this.gender = gender;
}
public String getIDNumber() {
return IDNumber;
}
public void setIDNumber(String IDNumber) {
if (IDNumber.length() != 18) {
JOptionPane.showMessageDialog(null, "身份证号输入有误,请重新输入!");
return;
}
this.IDNumber = IDNumber;
}
public int getScore() {
return score;
}
public void setScore(int score) {
if (score < 0 || score > 100) {
JOptionPane.showMessageDialog(null, "成绩输入有误,请重新输入!");
return;
}
this.score = score;
}
public String getScoreLevel() {
if (score >= 90) {
return "优秀";
} else if (score >= 80) {
return "良好";
} else if (score >= 70) {
return "中等";
} else if (score >= 60) {
return "及格";
} else {
return "不及格";
}
}
public void displayInfo() {
System.out.println("学号:" + id);
System.out.println("姓名:" + name);
System.out.println("性别:" + gender);
System.out.println("身份证号:" + IDNumber);
System.out.println("成绩:" + score + ",成绩等级:" + getScoreLevel());
}
}
public class TestStudent {
public static void main(String[] args) {
Student stu1 = new Student();
stu1.setId(JOptionPane.showInputDialog("请输入学号:"));
stu1.setName(JOptionPane.showInputDialog("请输入姓名:"));
stu1.setGender(JOptionPane.showInputDialog("请输入性别:"));
stu1.setIDNumber(JOptionPane.showInputDialog("请输入身份证号:"));
stu1.setScore(Integer.parseInt(JOptionPane.showInputDialog("请输入成绩:")));
stu1.displayInfo();
Student stu2 = new Student();
stu2.setId(JOptionPane.showInputDialog("请输入学号:"));
stu2.setName(JOptionPane.showInputDialog("请输入姓名:"));
stu2.setGender(JOptionPane.showInputDialog("请输入性别:"));
stu2.setIDNumber(JOptionPane.showInputDialog("请输入身份证号:"));
stu2.setScore(Integer.parseInt(JOptionPane.showInputDialog("请输入成绩:")));
stu2.displayInfo();
}
}
```
相关推荐
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)