永发信息网

JAVA:1、编写一个学生类,类名为Student,包含如下成员:

答案:2  悬赏:20  手机版
解决时间 2021-12-03 09:02
  • 提问者网友:酱爆肉
  • 2021-12-02 22:00
JAVA:1、编写一个学生类,类名为Student,包含如下成员:
最佳答案
  • 五星知识达人网友:千杯敬自由
  • 2021-12-02 22:38
public boolean setScore(double fenshu[]) {
    boolean result = true;
    for (int i = 0; i < fenshu.length; i++) {
        if (fenshu[i] < 0 || fenshu[i] > 100) {
            result = false;
        }
    } 
  if (result) {
        for (int i = 0; i < fenshu.length; i++) {
            this.score[i] = fenshu[i];
        }
    }
  return result;
}

public double[] max_min() {
    double[] result = new double[2];
    for (int i = 0; i < score.length; i++) {
        if (i == 0) {
            result[0] = score[i];
            result[1] = score[i];
        } else {
            if (score[i] > result[0]) {
         result[0] = score[i];
            } else {
         result[1] = score[i];
            }
        }
    }
    return result;
}追问请问在测试类中如何使用setScore方法为score数组赋初值?感谢!追答new一个新的double数组,把分数赋值,然后直接调用setScore方法就可以了
new Student student = new Student("张三", "001");
new double[] fenshu = new double[3];
fenshu[0] = 80;
fenshu[1] = 90;
fenshu[2] = 100;
if(student.setScore(fenshu)) {
    System.out.println("总分:" + student.sum());
    System.out.pritnln("平均分:" + student.average());
    System.out.println("最高分:" + student.max_min()[0]);
    System.out.rpitnln("最低分:" + student.max_min()[1]);
} else {
    System.out.println("分数异常!");
}
全部回答
  • 1楼网友:独钓一江月
  • 2021-12-03 00:16
//设置分数
public boolean setScore(double[] score) {
    //判断分数是否合法
    boolean ck=checkScore(score);
    //合法,返回true,并设置score
    if (ck){
        this.score = score;
        return true;
    }else{
        return false;
    }

}
//判断分数是否合法
public boolean checkScore(double[] score){
    for (int i=0;i          //数据不合法,直接返回
          if (score[i]<0||score[i]>100){
              return false;
          }
    }
    return true;
}
public double[] max_min(){
    //用于存放最大值和最小值
    double [] max_min=new double[2];
    //声明最大最小值,并附初始值
    double max=score[0];
    double min=score[0];
    for (int i=0;i         if(score[i]>max){
             max=score[i];
         }
         if(score[i]             min=score[i];
         }
    }
    max_min[0]=max;
    max_min[1]=min;
    return max_min;
}

//main函数赋值
double[]score=new double[]{20,30,40,50,60};
Student st=new Student("百度知道","001");
boolean bl= st.setScore(score);
if (bl){
    System.out.println("设置成功");
}else{
    System.out.println("设置失败,有异常数据!");
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯