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)