Educoder - Java入门 - 方法的使用各关卡题目总结
生活随笔
收集整理的這篇文章主要介紹了
Educoder - Java入门 - 方法的使用各关卡题目总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第1關 如何定義方法
package step1;public class HelloWorld {/********** Begin **********///定義一個方法,用來和老師打招呼/********** End **********/ public static void main(String[] args) {helloEducoder();}public static void helloEducoder() {System.out.println("hello teacher!");}/********** Begin **********/ //調用方法 /********** End **********/}第2關 掌握無參有返回值方法的調用
package setp7; public class HelloWorld {public static void main(String[] args) {/********** Begin **********/// 調用calcAvg()方法,并將返回值保存在變量avg中double avg =calcAvg(); /********** End **********/System.out.println("平均成績為:" + avg);}// 定義一個返回值為double類型的方法/********** Begin **********/public static double calcAvg() {double java = 92.5;double php = 83.0;double avg = (java + php) / 2; // 計算平均值return avg;// 使用return返回值/********** End **********/} }第3關 掌握有參數無返回值方法的調用
package setp9;import java.util.Scanner;public class HelloWorld {public static void main(String[] args) {HelloWorld hello = new HelloWorld();Scanner sc = new Scanner(System.in);int score1 = sc.nextInt(); //第一門成績int score2 = sc.nextInt(); //第二門成績/********** Begin **********/// 調用方法,傳入兩門課程的成績hello.calcAvg(score1,score2);/********** End **********/}/** 功能:計算兩門課程考試成績的平均分并輸出平均分* 定義一個包含兩個參數的方法,用來傳入兩門課程的成績*//********** Begin **********/public static void calcAvg(int score1,int score2){int avg=(score1+score2)/2;System.out.print("平均分:"+avg);}/********** End **********/}第4關 掌握有參數有返回值方法的調用
package step3;import java.util.Scanner;public class HelloWorld {public static void main(String[] args) {HelloWorld hello=new HelloWorld();Scanner sc = new Scanner(System.in);int i = sc.nextInt(); //獲取第一個輸入的整數int j = sc.nextInt(); //獲取第二個輸入的整數int max = hello.getMax(i,j);/********** Begin **********///在這里調用方法獲取返回值System.out.println( i+"和"+j+"比較,最大值是:" + max );/********** End **********/}/*在這里創建getMax方法,以兩個整數作為參數,返回兩個整數中較大的值*//********** Begin **********/public static int getMax(int i,int j){if(i>j) return i;else return j;}/********** End **********/ }第5關 掌握數組作為參數的使用
package setp10; import java.util.Arrays;public class HelloWorld {public static void main(String[] args) {HelloWorld hello=new HelloWorld();int[] scores={79,52,98,81};/********** Begin **********///調用方法,傳入成績數組,并獲取成績的個數int count=hello.sort(scores);/********** End **********/System.out.println("共有"+count+"個成績信息!");}/** 功能:將考試成績排序并輸出,返回成績的個數* *//********** Begin **********/ public static int sort( int[] data ){// 數組從小到大排序處理 for(int i=0;i<data.length-1;i++)for(int j=i+1;j<data.length;j++){if(data[i]>data[j]){int temp=data[i];data[i]=data[j];data[j]=temp;}}// 打印出數組System.out.print("[");for(int i=0;i<data.length-1;i++) System.out.print(data[i]+", "); System.out.print(data[3]+"]"); System.out.println();//返回數組中元素的個數return data.length;}/********** End **********/ }第6關 方法的重載
package setp15;/*** @author tangzhiqiang* @date 2018-04-25 22:31*/ public class HelloWorld {public static void main(String[] args) {/********** Begin **********/// 調用無參的方法print();// 調用帶有一個字符串參數的方法 print("educoder");// 調用帶有一個整型參數的方法print(666);/********** End **********/}/********** Begin **********/// 無參print方法的定義 public static void print(){System.out.println("無參的print方法"); }// 字符串print方法的定義 public static void print(String name){System.out.println("帶有一個字符串參數的print方法,參數值為:"+name);}// 整型print方法的定義 public static void print(int id){System.out.println("帶有一個整型參數的print方法,參數值為:"+id); }/********** End **********/ }第7關 選擇題
BDE
第8關 方法通關挑戰
package setp17; import java.util.Arrays; import java.util.Scanner; public class HelloWorld {/********** Begin **********//*** 第一題:定義一個方法 接收兩個整數類型的參數 a和b,返回兩個數的和 返回值類型為int 方法名為:getSum*/public static int getSum(int a ,int b){int sum = a + b;return sum;}/*** 第二題: 定義一個方法 接收三個double類型參數a,b,c, 返回這三個數的平均值 返回值類型為double 方法名為:getAvg*/public static double getAvg(double a ,double b,double c){double avg = (a + b + c) / 3;return avg;}/*** 第三題: 定義一個方法 接收兩個參數 a 和b 打印a行 b列的一個矩形 不需要返回值 方法名為:printRect*/public static void printRect(int a , int b){for (int i = 1 ; i <= a ;i++) {for (int k = 1 ; k <= b;k++){System.out.print("*");}System.out.println();}}/*** 第四題:定以一個方法,接收整形數組 為參數 對這個數組進行升序排序 最后輸出該數組 不需要返回值 方法名為 sortArr */public static void sortArr(int[] arr){for(int i = 0; i< arr.length-1;i++){for(int j = i+1; j< arr.length;j++){if(arr[i] > arr[j]){int tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;}}}for(int i = 0; i< arr.length;i++){System.out.println(arr[i]);}}/*** 第五題* 你只需要實現for 循環括號的內容就可 ,打印語句系統已經給你完成 */public static void Print99() {for (int i = 1 ; i<=9;i++) {for (int j = 1;j <= i ;j++) {System.out.print(j + " * " + i + " = " + i * j + "\t");}System.out.println();}}/********** End **********/ }總結
以上是生活随笔為你收集整理的Educoder - Java入门 - 方法的使用各关卡题目总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BurpSuite的https代理原理
- 下一篇: java美元兑换,(Java实现) 美元