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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

Felix_Sun 2019-07-29 23:41:03
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; ????}? ? 這道題 我想用有返回值的方式去解決,我這樣寫為什么老是報錯???
查看完整描述

1 回答

已采納
?
安浪創想

TA貢獻81條經驗 獲得超23個贊

這個代碼真亂啊啊啊

錯誤分析:函數返回時直接返回數據就行,不需要再定義類型了,只有初始定義時候才需要在變量前加int[]這樣的類型。

而且你的sort函數的返回值應該是 傳入的?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;

????}
}

而你的這個代碼不夠優化:

應該這樣優化:

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]);?這里是執行函數體,不要再這進行數據'使用'操作,應該把獲得的數據返回去而不是在這列出來
????????}
????????return?scores;
????}
}

執行結果為:

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

你的算法是反的了,sort函數已經從低到高排序,你取最大數據并獲得一個數據數組應該為:

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]);?這里是執行函數體,不要再這進行數據'使用'操作,應該把獲得的數據返回去而不是在這列出來
//????????????把最大的添加到數組里保存起來
????????????newTopScores.add(scores[i]);
????????}

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

運行結果為:

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


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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