java知識之方法的練習及注意事項

來源:互聯網
上載者:User

標籤:Java知識

1.1 方法的練習
1.1.1 方法練習之擷取兩個資料中的較大
1.1.2 案例代碼四

package com.itheima_01;import java.util.Scanner;/* * 需求:鍵盤錄入兩個資料,返回兩個數中的較大值 * * 兩個明確: * 傳回值類型:int * 參數列表:int a,int b */public class MethodTest {// 返回兩個數中的較大值public static int getMax(int a, int b) {if (a > b) {return a;} else {return b;}}public static void main(String[] args) {//建立對象Scanner sc = new Scanner(System.in);//接收資料System.out.println("請輸入第一個資料:");int x = sc.nextInt();System.out.println("請輸入第二個資料:");int y = sc.nextInt();//調用方法int max = getMax(x,y);System.out.println("max:"+max);}}

1.1.3 方法練習之比較兩個資料是否相等
1.1.4 案例代碼五

package com.itheima_01;import java.util.Scanner;/* * 需求:鍵盤錄入兩個資料,比較兩個數是否相等 * * 兩個明確: * 傳回值類型:boolean * 參數列表:int a,int b */public class MethodTest2 {//比較兩個數是否相等public static boolean compare(int a,int b){if(a==b){return true;}else {return false;}}public static void main(String[] args) {//建立對象Scanner sc = new Scanner(System.in);//接收資料System.out.println("請輸入第一個資料:");int a = sc.nextInt();System.out.println("請輸入第二個資料:");int b = sc.nextInt();//調用方法boolean flag = compare(a,b);System.out.println("flag:"+flag);}}

1.1.5 方法練習之擷取三個資料中的較大值
1.1.6 案例代碼六

package com.itheima_01;import java.util.Scanner;/* * 需求:鍵盤錄入三個資料,返回三個數中的最大值 * * 兩個明確: * 傳回值類型:int * 參數列表:int a,int b,int c */public class MethodTest3 {// 返回三個數中的最大值public static int getMax(int a, int b, int c) {if (a > b) {if (a > c) {return a;} else {return c;}} else {if (b > c) {return b;} else {return c;}}}public static void main(String[] args) {//建立對象Scanner sc = new Scanner(System.in);//接收資料System.out.println("請輸入第一個資料:");int a = sc.nextInt();System.out.println("請輸入第二個資料:");int b = sc.nextInt();System.out.println("請輸入第三個資料:");int c = sc.nextInt();//調用方法int max = getMax(a,b,c);System.out.println("max:"+max);}}

1.1.7 void修飾的方法的調用
寫一個方法,在控制台輸出10次HelloWorld案例。
沒有明確傳回值的函數調用:
其實就是void類型方法的調用
只能單獨調用
1.1.8 案例代碼七

package com.itheima_02;/* * 需求:寫一個方法,在控制台輸出10次HelloWorld案例。 * * 兩個明確: * 傳回值類型:void * 參數列表:無參數 * * 如果一個方法沒有明確的傳回值類型,java提供了void進行修飾。 * * void修飾的方法的調用: * A:單獨調用 */public class MethodDemo {//在控制台輸出10次HelloWorld案例。public static void printHelloWorld() {for(int x=1; x<=10; x++) {System.out.println("HelloWorld");}}public static void main(String[] args) {//單獨調用printHelloWorld();//輸出調用//System.out.println(printHelloWorld());//System.out.println(void);//賦值調用//void v = printHelloWorld();}}

1.1.9 列印1到n之間的資料
1.1.10 案例代碼八

package com.itheima_02;/* * 需求:寫一個方法,傳遞一個整數(大於1),在控制台列印1到該資料的值。 * * 兩個明確: * 傳回值類型:void * 參數列表:int n */public class MethodTest {//在控制台列印1到該資料n的值public static void printNumber(int n) {for(int x=1; x<=n; x++) {System.out.println(x);}}public static void main(String[] args) {printNumber(10);System.out.println("-------------------");printNumber(100);}}

1.1.11 列印所有的水仙花數
1.1.12 案例代碼九

package com.itheima_02;/* * 寫一個方法,把所有的水仙花數列印在控制台 * * 兩個明確: * 傳回值類型:void * 參數列表:無參數 */public class MethodTest2 {//把所有的水仙花數列印在控制台public static void printFlower() {for(int x=100; x<1000; x++) {int ge = x%10;int shi = x/10%10;int bai = x/10/10%10;if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == x){System.out.println(x);}}}public static void main(String[] args) {printFlower();}}

java知識之方法的練習及注意事項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.