求助

谛视 学生  2017-11-16 09:47:41
Java程序报错
int temp;
int nPaiMing[]={};
int nScores[]={365,401,406,455,389,378,376,303,289,405,
465,411,306,459,479,388,346,335,299,415,
485,403,407,469,471,438,396,345,369,408,
395,409,417,429,478,428,394,426,466,418,
393,463,429,464,441,458,386,395,348,447};
int nRanks[]={1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50};
String sNames[]={"陈奕","何春容","李琪霞","陈珍珍","虞喆斯","张颖","赵超群","吴丽萍","潘丹琴","巫梦婷",
"余梢婷","余丽君","胡梦诗","范玲娇","丰凯怡","张红梅","虞鑫悦","周佳慧","连青坡","沙孟",
"张道阳","陈宇航","陈瀚彝","陈明星","庄凌","胡清扬","劳一鸣","林总涛","陈斌","方开渊",
"胡斌","金阿青","马杰","董滨杰","马圆涛","姚仕纲","朱鸿扬","胡康鹏","崔俊杰","沃雪野",
"祝俊儿","张宏鑫","丁玲钰","彭嘉慧","毛利燕","周烯烯","吴霞芸","郑洁儿","徐书晴","杨丽珍"};
for (int i = 0;i < 48;i++){
for (int j = i + 1;j < 49;j++ ){
if (nScores[i] < nScores[j]){
temp = nScores[i];
nScores[i] = nScores [j];
nScores[j] = temp;
nRanks[i] = nRanks[j];
sNames[i] = sNames[j];
}
}
nPaiMing[i] = i+1;
}
for (int i = 0;i < 49;i++){
System.out.println("排名 " + nPaiMing[i] + " 学号 "+nRanks[i] + " 姓名 " + sNames[i] + " 分数 " + nScores[i]);
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at cn.java.exercises.PaiMing.main(PaiMing.java:32)
麻烦各位解答是哪里出错了
...全文
94 2 点赞 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Cathy313 2017-11-17
因为你没有初始化int nPaiMing[]={};的长度,所以不能插入数据。应该改为int nPaiMing[]=new int[50];你应该是想做排序吧?你的代码有问题。你的结果会覆盖掉一部分人的名字和学号。 (1)
 temp = nScores[i];
	                    nScores[i] = nScores [j];
	                    nScores[j] = temp;
	                    
	                    int temp2=nRanks[i];
	                    nRanks[i] = nRanks[j];
	                    nRanks[j]=temp2;
	                    
	                    String temp3=sNames[i];
	                    sNames[i] = sNames[j];
	                    sNames[j]=temp3;
(2)你的排名会少两个。 (3)你没有遍历完整,最好用nScores.length来代替明确的数字
  • 打赏
  • 举报
回复
李德胜1995 2017-11-16
int nPaiMing[]={};这是啥东西????
  • 打赏
  • 举报
回复
相关推荐
发帖
Java SE
加入

6.2w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2017-11-16 09:47
社区公告
暂无公告