課程
/后端開發
/Java
/Java入門第一季(IDEA工具)升級版
大佬大佬大佬教教我啊
2019-02-08
源自:Java入門第一季(IDEA工具)升級版 7-1
正在回答
import java.util.Arrays;
public class HelloWorld {
? ?
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ?int[] scores=new int[]{89,-23,64,91,119,52,73};
? ? HelloWorld hello=new HelloWorld();
? ? Arrays.sort(scores);
? ? int[] result=hello.calcScore(scores);
? ? ? System.out.println("考試成績的前三名為:");
? ? for(int i=0;i<result.length;i++){
? ? ? ? System.out.println(result[i]);
? ? }
? ? ??
? ? ? ??
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int[] calcScore(int[] scores){
? ? ? ? int[] tempArray=new int[3];
? ? ? ?int num=0;
? ? ? ?for(int i=scores.length-1;i>=0;i--){
? ? ? ? ? ?if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ?continue;
? ? ? ? ? ?}else{
? ? ? ? ? ? ? ?num+=1;
? ? ? ? ? ? ? ?if(num>3){
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ?for(int j=0;j<tempArray.length;j++){
? ? ? ? ? ? ? ? ? ? ? ?if(tempArray[j]==0){
? ? ? ? ? ? ? ? ? ? ? ? ? ?tempArray[j]=scores[i];
? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?return tempArray;
public?static?void?main(String[]?args)?{ ????//?show(?x?,?y)值傳遞,因為返回的是int,可以用int類型接收這個結果 ????int?sum?=?show(10,?20); ????//?打印結果 ????System.out.println(sum); } /** ?*?返回值類型?int ?*?參數?int?x,?int?y ?*/ public?static?int?show(int?x,?int?y){ ????int?z?=?x?+?y; ????//?將?x?+?y?結果返回 ????return?z; }
public?class?Test?{ ????public?static?void?main(String[]?args)?{ ????????//?show(?x?,?y)值傳遞,因為返回的是int,可以用int類型接收這個結果 ????????int?sum?=?show(10,?20); ????????//?打印結果 ????????System.out.println(sum); ????} ????/** ?????*?返回值類型?int ?????*?參數?int?x,?int?y ?????*/ ????public?static?int?show(int?x,?int?y){ ????????int?z?=?x?+?y; ????????//?將?x?+?y?結果返回 ????????return?z; ????} }
在方法名前定義返回類型,方法中寫關鍵字return + 該類型的參數結果。
你可用簡單理解為,別人問你一個什么結果,你在這個方法中給出這個結果就好了。
舉報
0基礎萌新入門第一課,從Java環境搭建、工具使用、基礎語法開始
2 回答求怎么寫這道題的帶返回值的代碼?謝謝
2 回答這題帶返回值怎么寫?
3 回答如果這題用帶返回值的方法改怎么編?。?/p>
1 回答代碼返回值的為什么不是布爾型
1 回答無參數無返回值的方法的使用,示例代碼運行錯誤
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2019-03-19
import java.util.Arrays;
public class HelloWorld {
? ?
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ?int[] scores=new int[]{89,-23,64,91,119,52,73};
? ? HelloWorld hello=new HelloWorld();
? ? Arrays.sort(scores);
? ? int[] result=hello.calcScore(scores);
? ? ? System.out.println("考試成績的前三名為:");
? ? for(int i=0;i<result.length;i++){
? ? ? ? System.out.println(result[i]);
? ? }
? ? ??
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int[] calcScore(int[] scores){
? ? ? ? int[] tempArray=new int[3];
? ? ? ?int num=0;
? ? ? ?for(int i=scores.length-1;i>=0;i--){
? ? ? ? ? ?if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ?continue;
? ? ? ? ? ?}else{
? ? ? ? ? ? ? ?num+=1;
? ? ? ? ? ? ? ?if(num>3){
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ?for(int j=0;j<tempArray.length;j++){
? ? ? ? ? ? ? ? ? ? ? ?if(tempArray[j]==0){
? ? ? ? ? ? ? ? ? ? ? ? ? ?tempArray[j]=scores[i];
? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?return tempArray;
? ? }
2019-02-12
2019-02-12
2019-02-08
在方法名前定義返回類型,方法中寫關鍵字return + 該類型的參數結果。
你可用簡單理解為,別人問你一個什么結果,你在這個方法中給出這個結果就好了。