亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

求大神幫我看下代碼,運行報錯

package zhuyong1;

import java.util.Arrays;

public class Yoo {

public static void main(String[] args) {

int[] scores={20,-52,39,96,57,45,43};

System.out.println("考試成績前三名為:");

Yoo hello=new Yoo();

hello.showTop3(scores);

}

public void showTop3(int[] scores){

Arrays.sort(scores);

int nums=0;

for(int i=scores.length-1; i>0;i--){

if(scores[0]<0||scores[i]>100){

continue;

}

nums++;

if(nums>3);{

break;

}

}

System.out.println(scores[i]);

}


}



正在回答

1 回答

1.

System.out.println(scores[i]);

這句中的i定義是在for循環里面,而System.out.println(scores[i]);在for外面,i沒有定義。

2.

if判斷語句中scores[0] = -52,滿足<0條件了就不會往下執行了。你最后得不到前三名的結果。

3.

我改了一下程序:

import?java.util.Arrays;

public?class?demo?{
	public?static?void?main(String[]?args)?{
		int[]?scores?=?{?20,?-52,?39,?96,?57,?45,?43?};
		System.out.println("考試成績前三名為:");
		showTop3(scores);
	}
	public?static?void?showTop3(int[]?scores)?{
		Arrays.sort(scores);
		for?(?int?i?=?scores.length?-?1;?i?>?scores.length-1-3;?i--)?{
			System.out.println(scores[i]);
		}
	}
}


1 回復 有任何疑惑可以回復我~
#1

zhuyong918 提問者

謝謝你,非常感謝,終于弄懂了。
2015-08-11 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

求大神幫我看下代碼,運行報錯

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號