Java語言中方法指的是封裝了一段特定邏輯功能的,使得程序結構清晰,便於代碼複用的代碼塊。java
修飾詞 返回返回值 方法名(參數列表){dom
方法體
}
*1. 返回值類型須要返回結果時使用,無返回結果時使用void
2.參數使用方法處理時靈活 無關緊要
3.有返回值必須寫return語句
4.return語句做用:返回結果並結束當前方法*spa
方法名();
方法名(參數值);code
public void say(){ System.out.println("Hello"); } say();//調用
public void say(String str){ System.out.println(str); } say("Hello");//調用
數據類型 變量=方法名();
數據類型 變量=方法名(參數值);blog
public int add(){ int c=3+2; return c; } int d=add();//調用
public int add(int a,int b){ int c=a+b; return c; } int d=add(5,4);//調用
注意事項:有返回值必須加return 並且return語句返回的值類型必須與返回值類型匹配。ip
Java語言流程控制包含了分支結果的控制和循環結構的控制。it
能夠把分支結構當作是自來水入戶:一個條總線(Main) + 一條/N條分線(If/IF...ELSE/SWITCH...CASE),總線中的水流會按照順序執行,到達分支(IF...語句)判斷是否須要分支(TRUE/FALSE),若是須要水(TRUE)就供水(進入循環),不須要水(FALSE)繼續執行代碼直至結束。要先作判斷再選擇的問題就要使用分支結構。io
int num=5; if(num%2==0){ System.out.println(num+"是偶數"); }
IF...ELSE 語句和IF語句差很少,都是走兩條路線,它們的區別在於IF...ELSE語句中對另一種語句也進行處理。for循環
if( 條件表達式 ){ ... }else{ ... }
平年閏年class
package com.mtingcat.javabasis.branchstructure03; import java.util.Scanner; /** *分支結構金典案例:平年閏年 * 分支條件: * 1.能被4整除,而且不能被100整除 * 2.能被400整除 * @author MTing * */ public class branchStructureIf { public static void main(String[] args) { System.out.println("年號:"); @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); int Y = scanner.nextInt(); if(Y%4==0&&Y%100!=0||Y%400==0){ System.out.println(Y + "年是閏年"); } else{ System.out.println(Y + "年是平年"); } } }
if( 條件表達式 ){ ... }else if( 條件表達式 ){ ... }else{ ... }
package com.mtingcat.javabasis.branchstructure03; import java.util.Scanner; /** * 商品打折 * 規則:滿10000打7折,滿20000打6折,滿30000打5折 * @author MTing * */ public class branchStructureIf02_discount { public static void main(String[] args) { System.out.println("消費總額:"); @SuppressWarnings("resource") double price = new Scanner(System.in).nextDouble(); double now = Discount(price); System.out.println(now); } public static double Discount(double price){ if ( price >= 10000 ){ price = price * 0.7; }else if ( price >=20000 ){ price = price * 0.6; }else if ( price > 30000){ price = price * 0.5; } return price; } }
package com.mtingcat.javabasis.branchstructure03; import java.util.Scanner; /** * 按照學生的成績給學生分等級 * 規則:100<= score >=90 優秀 * 80<= score < 90 良好 * 60<= score < 80 通常 * 0<= score <60 不及格 * @author MTing * */ public class branchStructureIf03_studentScore { public static void main(String[] args) { System.out.println("請輸入您的成績"); @SuppressWarnings("resource") double score = new Scanner(System.in).nextDouble(); if( score <= 100 && score >= 90 ){ System.out.println("您的成績屬於優秀"); }else if( score >= 80 && score < 90 ){ System.out.println("您的成績屬於良好"); }else if( score >= 60 && score < 80 ){ System.out.println("您的成績屬於通常"); }else if( score >= 0 && score < 60 ){ System.out.println("您的成績屬於不及格"); }else{ System.out.println("成績無效,請從新輸入"); } } }
當一個case成立,從這個case向後穿透全部case,包括default,直到程序結束或者遇到break程序才結束。
switch (key) { case value: break; default: break; }
package com.mtingcat.javabasis.branchstructure03; import java.util.Scanner; /** * 數字匹配 * @author MTing * */ public class branchStructureSwitch_dataMarry { public static void main(String[] args) { System.out.println("請輸入您的數字"); @SuppressWarnings("resource") int data = new Scanner(System.in).nextInt(); switch (data) { case 1: System.out.println("匹配1"); break; case 2: System.out.println("匹配2"); break; case 3: System.out.println("匹配3"); break; default: System.out.println("匹配錯誤"); break; } } }
循環指的是在程序中須要反覆執行的某個功能的程序代碼,由條件判斷是否退出循環,根據條件還能夠分紅先判斷後循環,先循環後判斷的結構
for (int i = 0; i < args.length; i++) { ... }
package com.mtingcat.javabasis.branchstructure03; /** * 輸出在1-1000中5的倍數 * @author MTing * */ public class branchStructureFor01_outPutData { public static void main(String[] args) { for (int i = 0; i < 1000; i++) { if(i % 5 == 0){ System.out.println(i); } } } }
for (int i = 0; i < args.length; i++) { for (int j = 0; j < args.length; j++) { ... } }
package com.mtingcat.javabasis.branchstructure03; /** * 九九乘法表 * @author MTing * */ public class branchStructureFor02_multiplicationTable { public static void main(String[] args) { System.out.println("九九乘法表啊"); for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { System.out.print(i +"*"+ j +"="+ i*j+"\t"); } System.out.println("\n"); } } }
for (int i = 0; i < args.length; i++) { if(判斷條件){ ... break;//若是成立,直接跳出這個for循環 } if(判斷條件){ ... continue;//若是成立,跳出本次for循環,進入下一輪 } }
package com.mtingcat.javabasis.branchstructure03; import java.util.Scanner; public class branchStructureFor03_continueBreak { public static void main(String[] args) { System.out.println("Your number:"); @SuppressWarnings("resource") int j = new Scanner(System.in).nextInt(); for (int i = 10; i > 0; i--) { if (j > i) { System.out.println("break"); break; } if (j < i) { System.out.println("continue"); continue; } } } }
先判斷後循環
while (en.hasMoreElements()) { type type = (type) en.nextElement(); }
package com.mtingcat.javabasis.branchstructure03; import java.util.Random; import java.util.Scanner; /** * 猜數字 * @author MTing * */ public class branchStructureFor03_guessData { public static void main(String[] args) { System.out.println("猜1-6的隨機數字"); int Data = new Random().nextInt(10)+1; while (true) { @SuppressWarnings("resource") int Input = new Scanner(System.in).nextInt(); if(Input > Data){ System.out.println("大了呦"); }else if(Input == Data){ System.out.println("正確"); }else{ System.out.println("小了小了"); } System.out.println("正確答案:" +Data); } } }
先循環後判斷
do { } while (condition);
package com.mtingcat.javabasis.branchstructure03; import java.util.Random; import java.util.Scanner; /** * 猜數字 * @author MTing * */ public class branchStructureFor03_guessData { public static void main(String[] args) { System.out.println("猜1-6的隨機數字"); int Data = new Random().nextInt(10)+1; do { @SuppressWarnings("resource") int Input = new Scanner(System.in).nextInt(); if(Input > Data){ System.out.println("大了呦"); }else if(Input == Data){ System.out.println("正確"); }else{ System.out.println("小了小了"); } System.out.println("正確答案:" +Data); } while (true); } }