定义一个学生Student类,有私有属性学号id、姓名name、年龄age、性别sex、学分score,定义一个构造方法初始化所有属性,show()方法显示信息;在主方法里创建学生对象,调用show()方法显示学生信息,根据学分段显示等级(<60:C,60-80:B,>80:A),格式:我叫大漂亮,学号20221234,女,20岁,成绩等级为A。(面向对象)
学生类
public class Student {
private int id;
private String name;
private int age;
private String sex;
private int score;
public Student(int id, String name, int age, String sex, int score) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score=score;
}
public void show(){
if(score<60){
System.out.println("我叫"+name+",学号"+id+","+sex+","+age+"岁,"+"成绩等级为:C");
}else if(score>=60&&score<=80){
System.out.println("我叫"+name+",学号"+id+","+sex+","+age+"岁,"+"成绩等级为:B");
}else if(score>80&&score<=100){
System.out.println("我叫"+name+",学号"+id+","+sex+","+age+"岁,"+"成绩等级为:A");
}else{
System.out.println("我叫"+name+",学号"+id+","+sex+","+age+"岁,"+"成绩输入有误");
}
}
}
主类
public class Application {
public static void main(String[] args) {
Student s1 =new Student(20221234,"大漂亮",20,"女",80);
s1.setName("大帅哥");
s1.setId(2019211471);
s1.setSex("男");
s1.setAge(21);
s1.setScore(90);
s1.show();
}
}
输出
我叫大帅哥,学号2019211471,男,21岁,成绩等级为:A