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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

求解啊 ,各位大佬大神??!

求解啊 ,各位大佬大神??!

import?java.util.Arrays; public?class?HelloWorld?{ ???? ????//完成?main?方法 ????public?static?void?main(String[]?args)?{ ????????System.out.println("考試成績的前三名為:"); ????????HelloWorld?hello?=?new?HelloWorld(); ?????? ????????int[]?scores?=?new?int[]{89,-23,64,91,119,52,73}; ????????int[]?nums?=?new?int[3]; ????????nums?=?hello.sort(scores); ???????? ???????? ???????? ????} ???? ????//定義方法完成成績排序并輸出前三名的功能 ????public?int[]?sort(int[]?scores){ ????????Arrays.sort(scores); ????????int?count?=?0; ????????for(int?i?=?scores.length-1;i>=0;i--){ ????????????if(scores[i]<0?||?scores[i]>100){ ????????????????continue; ???????????????? ????????????} ????????????else?{ ????????????????count++; ????????????} ????????????if(count>3){ ????????????????break; ????????????} ????????????System.out.println(scores[i]); ????????} ??????return?int[]?nums; ????}? ? 這道題 我想用有返回值的方式去解決,我這樣寫為什么老是報(bào)錯(cuò)?。?
查看完整描述

1 回答

已采納
?
安浪創(chuàng)想

TA貢獻(xiàn)81條經(jīng)驗(yàn) 獲得超23個(gè)贊

這個(gè)代碼真亂啊啊啊

錯(cuò)誤分析:函數(shù)返回時(shí)直接返回?cái)?shù)據(jù)就行,不需要再定義類型了,只有初始定義時(shí)候才需要在變量前加int[]這樣的類型。

而且你的sort函數(shù)的返回值應(yīng)該是 傳入的?scores,而不是前面過程中的nums

代碼如下:

import?java.util.Arrays;

public?class?HelloWorld?{


????//完成?main?方法

????public?static?void?main(String[]?args)?{

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

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


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

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

????????nums?=?hello.sort(scores);


????}


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

????public?int[]?sort(int[]?scores)?{

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

????????int?count?=?0;

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

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

????????????????continue;


????????????}?else?{

????????????????count++;

????????????}

????????????if?(count?>?3)?{

????????????????break;

????????????}

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

????????}

????????return?scores;

????}
}

而你的這個(gè)代碼不夠優(yōu)化:

應(yīng)該這樣優(yōu)化:

import?java.util.Arrays;

public?class?HelloWorld?{
????//完成?main?方法

????public?static?void?main(String[]?args)?{
????????System.out.println("考試成績的前三名為:");
????????int[]?scores?=?new?int[]{89,?-23,?64,?91,?119,?52,?73};
????????int[]?nums?=?new?int[3];
????????nums?=?sort(scores);
????????for?(int?rank?:
????????????????nums)?{
????????????System.out.println(rank);
????????}
????}
????//定義方法完成成績排序并輸出前三名的功能

????static?int[]?sort(int[]?scores)?{
????????Arrays.sort(scores);
????????int?count?=?0;
????????for?(int?i?=?scores.length?-?1;?i?>=?0;?i--)?{
????????????if?(scores[i]?<?0?||?scores[i]?>?100)?{
????????????????continue;
????????????}?else?{
????????????????count++;
????????????}
????????????if?(count?>?3)?{
????????????????break;
????????????}
//????????????System.out.println(scores[i]);?這里是執(zhí)行函數(shù)體,不要再這進(jìn)行數(shù)據(jù)'使用'操作,應(yīng)該把獲得的數(shù)據(jù)返回去而不是在這列出來
????????}
????????return?scores;
????}
}

執(zhí)行結(jié)果為:

考試成績的前三名為:
-23
52
64
73
89
91
119

你的算法是反的了,sort函數(shù)已經(jīng)從低到高排序,你取最大數(shù)據(jù)并獲得一個(gè)數(shù)據(jù)數(shù)組應(yīng)該為:

import?java.util.ArrayList;
import?java.util.Arrays;

public?class?HelloWorld?{
????//完成?main?方法

????public?static?void?main(String[]?args)?{
????????System.out.println("考試成績的前三名為:");
????????int[]?scores?=?new?int[]{89,?-23,?64,?91,?119,?52,?73};
????????ArrayList<Integer>?nums?=?new?ArrayList();
????????nums?=?sort(scores);
????????for?(int?i=1;i<=nums.size();i++){
????????????System.out.println("第"+i+"名:"+nums.get(i-1));
????????}
????}
????//定義方法完成成績排序并輸出前三名的功能

????static?ArrayList<Integer>?sort(int[]?scores)?{
????????Arrays.sort(scores);
????????int?count?=?0;
????????ArrayList<Integer>?newTopScores?=?new?ArrayList<>();
????????for?(int?i?=?scores.length?-?1;?i?>=?0;?i--)?{
????????????if?(scores[i]?<?0?||?scores[i]?>?100)?{
????????????????continue;
????????????}?else?{
????????????????count++;
????????????}
????????????if?(count?>?3)?{
????????????????break;
????????????}
//????????????System.out.println(scores[i]);?這里是執(zhí)行函數(shù)體,不要再這進(jìn)行數(shù)據(jù)'使用'操作,應(yīng)該把獲得的數(shù)據(jù)返回去而不是在這列出來
//????????????把最大的添加到數(shù)組里保存起來
????????????newTopScores.add(scores[i]);
????????}

????????return?newTopScores;
????}
}

運(yùn)行結(jié)果為:

考試成績的前三名為:
第1名:91
第2名:89
第3名:73


查看完整回答
1 反對(duì) 回復(fù) 2019-07-30
  • Felix_Sun
    Felix_Sun
    大佬 這么晚都在啊,辛苦啦。
  • Felix_Sun
    Felix_Sun
    可是有個(gè)地方?jīng)]看懂啊,怎么第一次優(yōu)化以后成績順序就反了呢? Arrays.sort(scores); int count = 0; for (int i = scores.length - 1; i >= 0; i--) 這個(gè)地方?jīng)]毛病???
  • 安浪創(chuàng)想
    安浪創(chuàng)想
    Arrays.sort(scores)就是一個(gè)排序了,而且操作了原來變量的內(nèi)存為升序了。如果要進(jìn)行降序,新的for循環(huán)重新生成一個(gè)數(shù)組按照自己降序的需要添加元素進(jìn)去,這里使用list,Java不允許修改數(shù)組長度。如果要數(shù)組定義也行,就前三名就定義三個(gè)元素長度的數(shù)組,依次修改元素為從大到小。 依次打印是可以按照降序打印的,但是函數(shù)的邏輯應(yīng)該是獲取是一個(gè)新的數(shù)據(jù)變量,而不是在函數(shù)中依次打印一遍就完了。所以我做了修改就是做個(gè)list生成一個(gè)新變量了
點(diǎn)擊展開后面3
  • 1 回答
  • 0 關(guān)注
  • 489 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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