第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

自己輸入,這個代碼錯在哪里呢

int[]?scores?=?new?int[5];
System.out.println("請輸入5個成績:");
for(int?j=1;j<=scores.length;j++){
????System.out.print("第"+j+"個學(xué)生的成績:");
????scores[j]?=?new?Scanner(System.in).nextInt();
}
int?min?=?scores[0];
int?max?=?scores[0];
double?avg=0;
int?sum=0;
for(int?i=0;i<scores.length;i++){
????if(min>scores[i])
????????min?=?scores[i];
????if(max<scores[i])
????????max?=?scores[i];
????sum+=scores[i];
????avg?=?sum/(scores.length);
}
System.out.println("最小值:"+min);
System.out.println("最大值:"+max);
System.out.println("平均值:"+avg);



請輸入5個成績:
第1個學(xué)生的成績:60
第2個學(xué)生的成績:70
第3個學(xué)生的成績:80
第4個學(xué)生的成績:65
第5個學(xué)生的成績:75
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
?? ?at HelloWorld.main(HelloWorld.java:13)
與目標(biāo) VM 斷開連接, 地址為: ''127.0.0.1:55891',傳輸: '套接字''

進(jìn)程已結(jié)束,退出代碼為 1

正在回答

3 回答

scores[j-1]?=?new?Scanner(System.in).nextInt();
可以這樣試試,原來的是因?yàn)閟cores數(shù)組從第二個位置也就是scores[1]開始填充數(shù)據(jù),數(shù)組超出界限報(bào)錯


0 回復(fù) 有任何疑惑可以回復(fù)我~
int[]?scores?=?new?int[5];
System.out.println("請輸入5個成績:");
//j應(yīng)該從0開始,不然你之前定義的長度new?int[5],就會從scores[1]開始,scores[0]就沒了
for(int?j=0;j<scores.length;j++){
????System.out.print("第"+(j+1)+"個學(xué)生的成績:");
????scores[j]?=?new?Scanner(System.in).nextInt();
}
int?min?=?scores[0];
int?max?=?scores[0];
double?avg=0;
int?sum=0;
for(int?i=0;i<scores.length;i++){
????if(min>scores[i])
????????min?=?scores[i];
????else?if(max<scores[i])
????????max?=?scores[i];
????sum+=scores[i];
????avg?=?sum/scores.length;
}
System.out.println("最小值:"+min);
System.out.println("最大值:"+max);
System.out.println("平均值:"+avg);


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕設(shè)計(jì)0259544

請輸入5個成績: 第1個學(xué)生的成績:60 第2個學(xué)生的成績:70 第3個學(xué)生的成績:80 第4個學(xué)生的成績:65 第5個學(xué)生的成績:75 最小值:60 最大值:80 平均值:70.0
2022-01-18 回復(fù) 有任何疑惑可以回復(fù)我~
?avg?=?sum/(scores.length);

這里不用括號

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
Java入門第一季(IDEA工具)升級版
  • 參與學(xué)習(xí)       1167745    人
  • 解答問題       18749    個

0基礎(chǔ)萌新入門第一課,從Java環(huán)境搭建、工具使用、基礎(chǔ)語法開始

進(jìn)入課程

自己輸入,這個代碼錯在哪里呢

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號