- class person
- {
- protected String name;
- protected char sex;
- protected int age;
- public void setName(String name)
- {
- this.name=name;
- }
- public String getName()
- {
- return name;
- }
- public void setSex(char sex)
- {
- this.sex=sex;
- }
- public char getSex()
- {
- return sex;
- }
- public void setAge(int age)
- {
- this.age=age;
- }
- public int getAge()
- {
- return age;
- }
- }
- class student extends person
- {
- private long number;
- private int chinese;
- private int math;
- private int english;
- public void setNum(long number)
- {
- this.number=number;
- }
- public long getNum()
- {
- return number;
- }
- public void setChi(int chinese)
- {
- this.chinese=chinese;
- }
- public int getChi()
- {
- return chinese;
- }
- public void setMat(int math)
- {
- this.math=math;
- }
- public int getMat()
- {
- return math;
- }
- public void setEng(int english)
- {
- this.english=english;
- }
- public int getEng()
- {
- return english;
- }
- public double aver()
- {
- int saver=(chinese+math+english)/3;
- return saver;
- }
- public String toString()
- {
- return "姓 名:"+getName()+"\n性 别:"+getSex()+"\n年 龄:"+getAge()+"\n学 号:"+getNum()+"\n平均分:"+aver();
- }
- }
- class stuTest
- {
- public static void main(String[] args)
- {
- student stu=new student();
- stu.setName("张三");
- stu.setSex('男');
- stu.setAge(20);
- stu.setNum(2812);
- stu.setChi(98);
- stu.setMat(100);
- stu.setEng(51);
- System.out.println(stu.toString());
- }
- }
复制代码 打印之后为什么平均分显示为83.0?定义的分数都是整型的,平均分也是整型的,打印后怎么带个小数点呢?内部难道有类型转换吗? |