首先简单介绍下,建立一个enum chengji枚举用来存储优良中差,并建立学生类stu,老师类tea ,测试类分别进行编写:
测试结果如下:
enum类:
public enum chengji {
/**90-100*/
优,
/**80-90*/
良,
/**60-80*/
中,
/**0-60*/
差
}
学生类stu:
package 成绩;
/**
* @author 葛帅帅
*
*/
public class stu {
private int id;
private String name;
private double fen;
private chengji cheng;
public stu(int id, String name, double fen) {
super();
this.id = id;
this.name = name;
this.fen = fen;
if(fen>=90&&fen<=100) {
this.cheng=chengji.优;
}
if(fen>=80&&fen<=90) {
this.cheng=chengji.良;
}
if(fen>=60&&fen<80) {
this.cheng=chengji.中;
}
if(fen>=0&&fen<60) {
this.cheng=chengji.差;
}
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getFen() {
return fen;
}
public chengji getCheng() {
return cheng;
}
}
老师类tea:
package 成绩;
public class tea {
private String name;
public tea(String name) {
super();
this.name = name;
}
public void dothing(stu s) {
System.out.print("我是"+s.getName()+"班主任,"+"我叫"+this.name+",对于此次他的成绩"+s.getFen()+",我认为:");
switch(s.getCheng()) {
case 中:
System.out.print("再接再厉!\n");
break;
case 优:
System.out.print("发奖状!\n");
break;
case 差:
System.out.print("假期补补课,别玩了!\n");
break;
case 良:
System.out.print("还有上升空间,加油!\n");
break;
default:
System.out.print("成绩有误,不能评级");
break;
}
}
}
测试类text:
package 成绩;
public class text {
public static void main(String[] args) {
stu s1=new stu(1001, "葛帅帅", 96);
stu s2=new stu(1002, "李心语", 83);
stu s3=new stu(1003, "陈玉龙", 26);
tea t=new tea("陈宇");
t.dothing(s1);
t.dothing(s2);
t.dothing(s3);
}
}
程序运行结果:
此程序适合Java初学者,主要用到switch,枚举类型,类的封装等小知识点,希望对你有所帮助,加油!