課程
/后端開發
/Java
/Java入門第一季(IDEA工具)升級版
寫了半天,求教一下,謝謝
2016-03-30
源自:Java入門第一季(IDEA工具)升級版 7-1
正在回答
返回值一次只能返回一條,所以要么你重復調用方法3次,輸出三次,要么你把前3名的成績轉成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;
}
舉報
0基礎萌新入門第一課,從Java環境搭建、工具使用、基礎語法開始
2 回答求怎么寫這道題的帶返回值的代碼?謝謝
3 回答如果這題用帶返回值的方法改怎么編啊?
2 回答用帶參帶返回值不能完成這個嗎?
1 回答方法何時帶返回值
2 回答自己試了一下不行,請問改為帶參帶返回值應該怎么寫?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-03-31
返回值一次只能返回一條,所以要么你重復調用方法3次,輸出三次,要么你把前3名的成績轉成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;
? ? }
? ??
}