課程
/后端開發(fā)
/Java
/Java入門第一季(IDEA工具)升級(jí)版
寫了半天,求教一下,謝謝
2016-03-30
源自:Java入門第一季(IDEA工具)升級(jí)版 7-1
正在回答
返回值一次只能返回一條,所以要么你重復(fù)調(diào)用方法3次,輸出三次,要么你把前3名的成績轉(zhuǎn)成String型返回
慕仙1776787 提問者
慕粉1463572084
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? int[] scores={89,-23,64,91,119,52,73};
? ? ? ? int[] topThree=hello.getScores(scores);
? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? for(int top:topThree)
? ? ? ? System.out.println(top);
? ? }
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int[] getScores(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int[] newScores=new int[3];
? ? ? ? for(int i=scores.length-1,j=0; i>=0&&j<=2; i--){
? ? ? ? ? ? if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? newScores[j]=scores[i];
? ? ? ? ? ? j++;
? ? ? ? }
? ? ? ? return newScores;
}
舉報(bào)
0基礎(chǔ)萌新入門第一課,從Java環(huán)境搭建、工具使用、基礎(chǔ)語法開始
2 回答求怎么寫這道題的帶返回值的代碼?謝謝
3 回答如果這題用帶返回值的方法改怎么編???
2 回答用帶參帶返回值不能完成這個(gè)嗎?
2 回答自己試了一下不行,請(qǐng)問改為帶參帶返回值應(yīng)該怎么寫?
1 回答方法何時(shí)帶返回值
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-03-31
返回值一次只能返回一條,所以要么你重復(fù)調(diào)用方法3次,輸出三次,要么你把前3名的成績轉(zhuǎn)成String型返回
2016-06-03
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? int[] scores={89,-23,64,91,119,52,73};
? ? ? ? int[] topThree=hello.getScores(scores);
? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? for(int top:topThree)
? ? ? ? System.out.println(top);
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int[] getScores(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int[] newScores=new int[3];
? ? ? ? for(int i=scores.length-1,j=0; i>=0&&j<=2; i--){
? ? ? ? ? ? if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? newScores[j]=scores[i];
? ? ? ? ? ? j++;
? ? ? ? }
? ? ? ? return newScores;
? ? }
? ??
}