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

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

用return方法怎么寫

小伙伴們,請根據(jù)所學(xué)知識,編寫一個 JAVA 程序,實現(xiàn)輸出考試成績的前三名

要求:

1、 考試成績已保存在數(shù)組 scores 中,數(shù)組元素依次為 89 , -23 , 64 , 91 , 119 , 52 , 73

2、 要求通過自定義方法來實現(xiàn)成績排名并輸出操作,將成績數(shù)組作為參數(shù)傳入

3、 要求判斷成績的有效性( 0—100 ),如果成績無效,則忽略此成績

運行效果:


正在回答

2 回答

public static void main(String[] args) {

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

? ? ? ? HelloWorld hello = new ?HelloWorld();

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

? ? ? ? int[] topScores = new int[3]; ?//新建一個數(shù)組來保存最高三個數(shù)組

? ? ? ?topScores = hello.highScore(scores);

? ? ? ?for(int i=0;i < topScores.length;i++)

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

? ? ? ?

? ? ? ?

? ?}

??

? ?//定義方法完成成績排序并輸出前三名的功能

? ?public int[] highScore(int[] scores)

? ?{

? ? ? ?Arrays.sort(scores);

? ? ? ?int flag = 0;

? ? ? ?int[] topScores = new int[3];

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

? ? ? ?{

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

? ? ? ? ? ?{

? ? ? ? ? ? topScores[flag] = scores[i]; ? //如果是最高的,就保存在這個長度為3的數(shù)組中,最后用作返回值。

? ? ? ? ? ? flag++;

? ? ? ? ? ?}

? ? ? ? ? ?if(flag == 3)

? ? ? ? ? ? break;

? ? ? ?}

? ? ? ?return topScores;

? ?}


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

?//定義方法完成成績排序并輸出前三名的功能
??? public void ranking(int[] scores)
??? {
??????? int count = 0;
??????? for(int i = scores.length - 1;i >= 0; i--)
??????? {
?????????? ?
??????????? if(scores[i] < 100 && count <3)
??????????? {
??????????????? System.out.println("" + scores[i]);
??????????? }
??????????? if(count == 3)
??????????? {
??????????????? break;
??????????? }
??????? }
??? }
???

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

舉報

0/150
提交
取消

用return方法怎么寫

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

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

幫助反饋 APP下載

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

公眾號

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