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

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

找不到哪錯了,求大神指教!


import java.util.Arrays;

public class HelloWorld {

?? ?public static void main(String[] args) {
?? ??? ???????? int scores[]=new int[]{89,-23,64,91,119,52,73};
?? ??? ??? ??? ?HelloWorld Hello=new HelloWorld();
?? ??? ??? ??? ?System.out.println("前三名成績是:");
?? ??? ???????? Hello.rangKing(scores);
?? ??? ???? ?
?? ??? ???? }
?? ??? ??? ?
?? ??? ???? //定義方法完成成績排序并輸出前三名的功能
?? ??? ???? public void rangKing(int scores[]){
?? ??? ??? ??? ?Arrays.sort(scores);
?? ??? ???????? for(int i=scores.length;i>=0;i--){
?? ??? ??????? ??? ?int num=0;
?? ??? ???????????? if(scores[i]<0||scores[i]>100)
?? ??? ??????????? ??? ?continue;
?? ??? ???????????? num++;
?? ??? ???????????? if(num<=3){
?? ??? ???????????? System.out.println(scores[i]);
?? ??? ???????????? }
?? ??? ???????? }
?? ??? ??????? ?
?? ??? ???????? }
?? ??? ???? }
???

正在回答

5 回答

i=scores.length-1,如果是=scores.length的話,會報數(shù)組越界的吧

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

夏木啊 提問者

非常感謝!
2016-08-10 回復(fù) 有任何疑惑可以回復(fù)我~

? ? public void rangKing(int scores[]){

? ? ? ? ? ? ? ? Arrays.sort(scores);

? ? ? ? ? ? ? ? ?int num=0;

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

? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? if(scores[i]>0&&scores[i]<100)

? ? ? ? ? ? ? ? ? ? {num++;

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

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? if(num==3)break;

? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }


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

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

這句錯了,i=scores.length越界了,應(yīng)該改成i=scores.length-1

0 回復(fù) 有任何疑惑可以回復(fù)我~
1.for(int?i=scores.length;i>=0;i--){
????scores.length應(yīng)該改為scores.length-1;此數(shù)組中沒有第7位元素,只有0,1,2,3,4,5,6;
2.int?num=0;
????num的定義要移到for循環(huán)外;
3.??if(num<=3){
???????System.out.println(scores[i]);
????}
????1)應(yīng)該是num>3。這是一個判斷,取三個成績就跳出循環(huán),不再執(zhí)行,因此
????2)if語句的語句塊應(yīng)該是break,跳出循環(huán)
????3)System.out.println(scores[i]);這個輸出語句要移到if語句外,但要在for循環(huán)中

正確代碼:
package?day02;

import?java.util.Arrays;

public?class?Test06?{

	public?static?void?main(String[]?args)?{
		int[]?scores?=?{89?,?-23?,?64?,?91?,?119?,?52?,?73};
		Test06?t?=?new?Test06();
		t.showTop3(scores);
	}
	public?void?showTop3(int[]?scores){
		Arrays.sort(scores);
		int?num?=?0;
		for(int?i?=?scores.length-1;i>=0;i--){
			
			if(scores[i]<0||scores[i]>100){
				continue;
			}
			num++;
			if(num>3){
				break;
			}
			System.out.println(scores[i]);
		}
	}
}


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

...大胸弟,if(num<=3)...這不是循環(huán)語句。,

  1. 一般不確定循環(huán)次數(shù)的用while

  2. 確定循環(huán)次數(shù)的用for ,switch

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

舉報

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

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

進入課程

找不到哪錯了,求大神指教!

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

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

幫助反饋 APP下載

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

公眾號

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