最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

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

搞不清出了什么問題,求大神

public class hallo {

public static void main(String[] args){

?int i,j,k;

?int score[]={89,-23,64,91,119,52,73};

?for(i=0;i<7;i++){

?for(j=0;j<7;j++){

?if(score[j]<score[j+1]){

?k=score[j];

?score[j+1]=score[j];

?score[j+1]=k;

? }

?}

? }for(i=0;i<3;i++)

System.out.println("score[i]");

? }

}

出現(xiàn)Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7怎么解決

正在回答

2 回答

這個報錯是說數(shù)組下表超出范圍,以后記住這個關(guān)鍵詞“ArrayIndexOutOfBounds”(數(shù)組索引超出界限)。

那么問題在哪里呢?看了下你寫這個程序目的是想比對score[j]<score[j+1]時,將數(shù)組值對調(diào)位置,實現(xiàn)排序目的。但是試想一下,數(shù)組數(shù)量是7個,下標(biāo)是0到6,那你循環(huán)了7次,最后一次下標(biāo)是6,就出現(xiàn)了score[6]<score[7],請問哪里有score[7]?

for(i = 0; i < 7; i++){
???for(j = 0; j < 7; j++){

將這里的7改成6就不會報錯,但是這個程序來看是有問題的計算出來的結(jié)果并不數(shù)你想要的排序。

在JAVA里有一個方法可以直接對數(shù)組排序,java.uitl.Arrays里面的sort()方法,你可以看看第6章6-7節(jié)的內(nèi)容。


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

Jharden 提問者

非常感謝!
2016-02-28 回復(fù) 有任何疑惑可以回復(fù)我~
import?java.util.Arrays;
public?class?HelloWorld?{
????
????//完成?main?方法
????public?static?void?main(String[]?args)?{
???????int?scores[]={89,-23,64,91,119,52,73};?
????????HelloWorld?hello=new?HelloWorld();
????????hello.forword(scores);
????????
????}
????//定義方法完成成績排序并輸出前三名的功能
????public?void?forword(int[]?scores){
????	int?count=0;
????????Arrays.sort(scores);
????????System.out.println("考試成績的前三名為:");
????????for?(int?i?=scores.length-1;?i>=0;?i--)?{
			if(scores[i]>0){
				count++;
				System.out.println(scores[i]);
				if(count>2)
					break;
			}
		}
????}
}


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

舉報

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

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)微信公眾號