weixin_48362397 2021-05-26 21:59 采纳率: 33.3%
浏览 22
已采纳

代码纠错,类与对象,,

package Item_10;

import java.util.Scanner;

import static Item_10.setScore.getFinalScore;

public class check {
    Scanner in = new Scanner(System.in);

    public player inputPlayer() {
        player p = null;
        setScore[]sc=new setScore[10];
        System.out.println("输入选手信息(编号,姓名):");
        p = new player(in.next(), in.next());
        System.out.println("输入评委打分:");
        for(int i=0;i<10;i++){
            sc[i]=new setScore();
            sc[i].setScore(in.nextDouble());
        }
        p.setScore(getFinalScore(sc));
        return p;
    }

    public void deletePlayer(player[] p, String no) {
        for (int i = 0; i < p.length; i++) {
            if (p[i].no.compareTo(no)==0) p[i] = null;
        }
        System.out.println("删除成功!");
    }

    public void updatePlayer(player[] p, String no, String name, double grade) {
        for (int i = 0; i < p.length; i++) {
            if (p[i].no.compareTo(no)==0) {
                p[i].setName(name);
                p[i].setScore(grade);
            }
        }
        System.out.println("修改成功!");
    }
}
package Item_10;

public class getScore {
//    int index;
//
//    public getScore() {
//    }
//
//    public getScore(int index) {
//        this.index = index;
//    }
    public void displayAllPlayer(player[]p,int index){
        //按选手序号排名
        player p1=new player();
        if(index==1){
            for (int i=0;i<p.length;i++){
                System.out.println(p[i]);
            }
        }
        else {
            for(int i=0;i<p.length;i++){
                for(int j=0;j<p.length-i;j++) {
                    if (p[i].score < p[i + 1].score) {
                        p1=p[i];
                        p[i] = p[i + 1];
                        p[i+1]=p1;
                    }
                    }
                }
            }
        }
    }

 

package Item_10;

import java.util.Scanner;

public class item10 {
    public static void main(String[] args) {
        check ck = new check();
        getScore gs = new getScore();
        Scanner in = new Scanner(System.in);
        player[] per;
        System.out.println("please input the number of players:");
        int t = in.nextInt();
        per = new player[t];
        int x = 10;
        System.out.println("1.录入选手信息\n2.删除对应编号选手信息\n3.显示选手成绩\n0.退出");
        while (x != 0) {
            x = in.nextInt();
            switch (x) {
                case 1:
                    for (int i = 0; i < t; i++) {
                        per[i] = ck.inputPlayer();
                    }
                    break;
                case 2:
                    System.out.println("输入选手编号");
                    ck.deletePlayer(per, in.next());
                    break;
                case 3:
                    System.out.println("1.以选手序号排序\n2.以选手成绩排序");
                    gs.displayAllPlayer(per, in.nextInt());
            }
        }
    }
}
package Item_10;

public class player {
    String no;
    String name;
    double score;

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    @Override
    public String toString() {
        return "player{" +
                "no='" + no + '\'' +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public player(String no, String name) {
        this.no = no;
        this.name = name;
    }

    public player(String no, String name, double score) {
        this.no = no;
        this.name = name;
        this.score = score;
    }

    public player() {
    }
}
package Item_10;

public class setScore {
    double score;

    public setScore(double score) {
        this.score = score;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public setScore() {
    }

    public static double getFinalScore(setScore[] s) {
        double finalScore=0;
        setScore s1=new setScore();
        for(int i=0;i<s.length;i++){
            for(int j=0;j<s.length-i;j++) {
                if (s[i].score < s[i + 1].score) {
                    s1=s[i];
                    s[i] = s[i + 1];
                    s[i+1]=s1;
                }
            }
        }
        for(int j=1;j<s.length-1;j++){
            finalScore+=s[j].score;
        }
        return finalScore/8;
    }

}

输入了10个分数之后有错,求帮忙纠错,看半天觉得数组这没问题啊

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
    at Item_10.setScore.getFinalScore(setScore.java:26)
    at Item_10.check.inputPlayer(check.java:20)
    at Item_10.item10.main(item10.java:21)

 

 

 

 

  • 写回答

2条回答 默认 最新

  • 会写代码的花城 2021-05-28 00:35
    关注

    你的

     if (s[i].score < s[i + 1].score) {

    这段i+1是会越界的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划