#####通过以下代码运行输入实现功能一成绩录入时出现
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at ManagementSystem.system.main(system.java:46)
(明明在网上查阅后也是这样写的,为什么网上的能运行,我的就运行不出来)
package ManagementSystem;
import java.util.*;
public class system {
private static final int MAXITEM=3;
private static final int MAXNUMBER=100;
public static void main(String[] args) {
Athlete[][] man=new Athlete[MAXITEM][MAXNUMBER];
Recorder re1=new Recorder("李文",23.4); //保持项目的纪录
Recorder re2=new Recorder("冯伟",23.39);
Recorder re3=new Recorder("戴瑞",5.33);
int n[]=new int[3];
Scanner sc=new Scanner(System.in);
while(true) { //循环菜单界面
System.out.println("\n-------------菜单(menu)-------------");
System.out.println("1.录入运动员成绩;");
System.out.println("2.每个项目的平均成绩、最好成绩以及最差成绩;");
System.out.println("3.每个项目的成绩按降序排序;");
System.out.println("4.判断每个项目是否有破纪录的运动员;");
System.out.println("5.查询运动员成绩。");
System.out.println("\n*请输入对应序号,进入功能。输入0结束运行");
System.out.println("------------------------------------");
int choice=sc.nextInt(); //输入选择的功能
int i,j;
switch(choice) {
case 0:
System.exit(0);
break;
//成绩录入
case 1:
System.out.println("\n---------------录入成绩---------------");
for(i=0;i<3;i++) {
System.out.print("请输入要录入项目"+(i+1)+"的人数:");
n[i]=sc.nextInt();
System.out.println(" ------项目"+(i+1)+"------");
if(i==1)
System.out.println("参赛号码 姓名 成绩(s)");
else
System.out.println("参赛号码 姓名 成绩(m)");
for(j=0;j<n[i];j++) {
Athlete athlete=new Athlete();
String s=sc.next(); //获取用户输入
String[] s1=s.split(" "); //通过split切割字符串,按行读取数据,用空格分隔1
athlete.SetNumber(s1[0]);
athlete.SetName(s1[1]);
athlete.SetGrade(Double.parseDouble(s1[2]));
man[i][j]=athlete; //将输入的数据保存到运动员数组中
}
}
System.out.println("\n---------------录入成绩---------------");
break;
//成绩计算
case 2:
double ave=0,best=0,worse;
System.out.println("\n---------------计算成绩---------------");
System.out.println("\n项目 平均成绩 最好成绩 最差成绩");
for(i=0;i<3;i++) {
worse=man[i][0].GetGrade();
j=0;
for(j=0;j<n[i];j++) {
ave+=man[i][j].GetGrade();
if(best<man[i][j].GetGrade() && i!=1) //求最好成绩
best=man[i][j].GetGrade();
else if(best>man[i][j].GetGrade() && i==1)
best=man[i][j].GetGrade();
if(worse>man[i][j].GetGrade() && i!=1) //求最差成绩
worse=man[i][j].GetGrade();
else if(best>man[i][j].GetGrade() && i==1)
best=man[i][j].GetGrade();
}
ave=ave/n[i];
System.out.println((i+1)+" "+String.format("%.2f", ave)+" "+String.format("%.2f", best)+" "+String.format("%.2f", worse));
}
System.out.println("\n---------------计算成绩---------------");
break;
//成绩排序
case 3:
int k,l;
System.out.println("\n---------------成绩排序---------------"); //选择排序
for(i=0;i<3;i++) {
System.out.println(" ------项目"+(i+1)+"------");
if(i==1)
System.out.println("参赛号码 姓名 成绩(s)");
else
System.out.println("参赛号码 姓名 成绩(m)");
for(j=0;j<n[i]-1;j++) {
k=j;
for(l=j+1;l<n[i];l++) {
if(man[i][j].GetGrade()<man[i][l].GetGrade() && i!=1)
k=l;
else if(man[i][j].GetGrade()>man[i][l].GetGrade() && i==1)
k=l;
}
if(k!=j) {
Athlete ath=new Athlete();
ath=man[i][j];
man[i][j]=man[i][k];
man[i][k]=ath;
}
System.out.println(man[i][j].GetNumber()+" "+man[i][j].GetName()+" "+String.format("%.2f",man[i][j].GetGrade()));
}
System.out.println(man[i][j].GetNumber()+" "+man[i][j].GetName()+" "+String.format("%.2f",man[i][j].GetGrade()));
}
System.out.println("\n---------------成绩排序---------------");
break;
//破纪录
case 4:
System.out.println("\n--------------是否破纪录?--------------");
System.out.println("纪录保持者:\n项目一:"+re1.GetMname()+" "+String.format("%.2f", re1.GetRecord()));
System.out.println("项目二:"+re2.GetMname()+" "+String.format("%.2f", re2.GetRecord()));
System.out.println("项目三:"+re3.GetMname()+" "+String.format("%.2f", re3.GetRecord()));
for(i=0;i<3;i++) {
for(j=0;j<n[i];j++) {
if(man[i][j].GetGrade()>re1.GetRecord() && i==0 || man[i][j].GetGrade()>re3.GetRecord() && i==2) {
if(i==0) {
re1.SetMname(man[i][j].GetName());re1.SetRecord(man[i][j].GetGrade());
}
else {
re3.SetMname(man[i][j].GetName());re3.SetRecord(man[i][j].GetGrade());
}
System.out.println("项目"+(i+1)+"有破纪录者:"+man[i][j].GetNumber()+" "+man[i][j].GetName()+" "+String.format("%.2f", man[i][j].GetGrade()));
}
else if(man[i][j].GetGrade()<re2.GetRecord() && i==1) {
re2.SetMname(man[i][j].GetName());re2.SetRecord(man[i][j].GetGrade());
System.out.println("项目"+(i+1)+"有破纪录者:"+man[i][j].GetNumber()+" "+man[i][j].GetName()+" "+String.format("%.2f", man[i][j].GetGrade()));
}
else
System.out.println("项目"+(i+1)+"没破记录");
}
}
System.out.println("\n--------------是否破纪录?--------------");
break;
//查询
case 5:
int c=0,num=0;
while(c==0) {
System.out.println("\n---------------查询成绩---------------");
System.out.print("请输入参赛号码:");
String s=sc.next();
for(i=0;i<3;i++) {
for(j=0;j<n[i];j++) {
if(man[i][j].GetNumber().equals(s)) {
System.out.println(man[i][j].GetNumber()+" "+man[i][j].GetName()+" "+man[i][j].GetGrade());
num++;
}
}
}
if(i==4 && num==0)
System.out.println("该参赛号码不存在!!!");
System.out.println("\n---------------查询成绩---------------");
System.out.println("\n*继续查询0/返回菜单1");
c=sc.nextInt();
}
break;
default:
System.out.println("输入错误!!请重新输入!!!");
break;
}
}
}
}
package ManagementSystem;
public class Athlete{ //运动员类
private String name,number; //运动员姓名、参赛号码
private double grade; //运动员成绩
public void SetName(String name) { //设置姓名
this.name=name;
}
public void SetNumber(String number) { //设置参赛号码
this.number=number;
}
public void SetGrade(double grade) { //设置成绩
this.grade=grade;
}
public String GetName() { //获取姓名
return name;
}
public String GetNumber() { //获取参赛号码
return number;
}
public double GetGrade() { //获取成绩
return grade;
}
}
package ManagementSystem;
public class Recorder {
private String mname;
private double record;
Recorder(String mname,double record) {
this.mname=mname;
this.record=record;
}
public void SetMname(String mname) {
this.mname=mname;
}
public void SetRecord(double record) {
this.record=record;
}
public String GetMname() {
return mname;
}
public double GetRecord() {
return record;
}
}
######求修改,希望能够解决,谢谢,拜托了