java吧 关注:1,274,541贴子:12,788,072
  • 6回复贴,共1

为什么这个输出结果不对,请大神看下

只看楼主收藏回复

public class StudentBean {
private String name;
private int age;
private String sex;
private boolean flag;
private double score;
public StudentBean(){
}
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 boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
public abstract class StudentManage{
protected StudentBean stubean[];
public abstract void handleStudentInfo();
public abstract void showAllStudentInfo();
}
public class StudentUpdate extends StudentManage{
public StudentUpdate(){
Scanner sc=new Scanner(System.in);
StudentBean stubean[]=new StudentBean[3];
for (int i = 0; i <3; i++) {
stubean[i]=new StudentBean();
System.out.println("请输入学生的姓名");
stubean[i].setName(sc.next());
System.out.println("请输入学生的性别");
stubean[i].setSex(sc.next());
System.out.println("请输入学生的年龄");
stubean[i].setAge(sc.nextInt());
System.out.println("请输入学生的名族是为true");
stubean[i].setFlag(sc.nextBoolean());
System.out.println("请输入学生的分数");
stubean[i].setScore(sc.nextInt());
}
}
@Override
public void handleStudentInfo() {
StudentBean stubean[]=new StudentBean[3];
for (int i = 0; i <3; i++) {
stubean[i]=new StudentBean();
if(stubean[i].isFlag()==true){
stubean[i].setScore(stubean[i].getScore()+20);
}else{
stubean[i].setScore(stubean[i].getScore()+10);
}
}
}
@Override
public void showAllStudentInfo() {
StudentBean stubean[]=new StudentBean[3];
for (int i = 0; i < 3; i++) {
stubean[i]=new StudentBean();
System.out.print(stubean[i].getName()+"\t");
System.out.print(stubean[i].getAge()+"\t");
System.out.print(stubean[i].getSex()+"\t");
System.out.print(stubean[i].isFlag()+"\t");
System.out.print(stubean[i].getScore()+"\t");
System.out.println();
}
}
}
public class StudentTest {
public static void main(String[] args) {
StudentUpdate s=new StudentUpdate();
System.out.println("-------实现前的数据----------");
System.out.println("姓名"+"\t"+"性别"+"\t"+"年龄"+"\t"+"民族"+"\t"+"分数");
s.showAllStudentInfo();
s.handleStudentInfo();
System.out.println("-------实现后的数据----------");
System.out.println("姓名"+"\t"+"性别"+"\t"+"年龄"+"\t"+"民族"+"\t"+"分数");
s.showAllStudentInfo();
}
}
输出结果为什么是这个
-------实现前的数据----------
姓名性别年龄民族分数
null0nullfalse0.0
null0nullfalse0.0
null0nullfalse0.0
-------实现后的数据----------
姓名性别年龄民族分数
null0nullfalse0.0
null0nullfalse0.0
null0nullfalse0.0


IP属地:浙江1楼2014-05-16 00:01回复
    sc.next() 换成sc.nextLine()


    2楼2014-05-16 00:18
    回复
      2025-08-18 23:14:34
      广告
      不感兴趣
      开通SVIP免广告
      你每次都重新new的数组对象。


      IP属地:安徽来自Android客户端3楼2014-05-16 06:23
      收起回复
        这些输出的都是各个数据类型的默认值。


        IP属地:内蒙古来自iPhone客户端4楼2014-05-16 08:11
        收起回复
          好像你设的是isFLag并没有get方法,楼主以后不要手写了,在ide里面重构一下代码就会自己加上get和set方法,避免了手输出错的问题


          来自iPhone客户端5楼2014-05-16 08:37
          回复