java 流程控制基礎之if else
阿新 • • 發佈:2020-07-22
分支結構
根據條件選擇性執行某段程式碼;有if...else和switch-case兩種分支
迴圈結構
根據迴圈條件迴圈執行某段程式碼;有while do while 、for 三種迴圈語句;注JDK1.5提供了foreach迴圈。遍歷集合、陣列元素
分支結構介紹
if 語句的三種格式
1. if (條件表示式){ //表示式結果為真就執行程式碼塊的;否則直接跳過不執行
執行的程式碼塊;
}
2. if(條件表示式){ //表示式結果為真就執行後面的程式碼塊;否則執行else後的程式碼塊
執行的程式碼塊;
}else{
執行的程式碼塊;
}
3.if(條件表示式){ //條件表示式成立;就執行;否則就繼續
程式碼塊;
}
else if (條件表示式2){ // 執行程式碼塊成立;
程式碼塊;
}
......
else{ //前面的都沒有執行這個最後執行
程式碼塊;
}
/* 分支結構中的if -else(條件判斷結構) 第一種:三種結構 if(條件表示式){ } 第二種,二選一 if(條件表示式){ }else{ 程式碼塊; } 第三種多選一 if(條件表示式){ 程式碼塊; }else if(條件表示式){ 程式碼塊2; }else if (條件表示式){ 執行程式碼塊3; } .... else{ 執行程式碼n; } */ class IfTest { public static void main(String[] args) { int heartBeats = 79; if (heartBeats < 60 || heartBeats > 100) { System.out.println("需要做進一步檢查"); } System.out.println("檢查結束"); //舉例二選一 int age = 23; if ( age<18 ) { System.out.println("你還可以看動畫片"); }else{ System.out.println("你可以看成人了"); } //多選一 if (age < 0) { System.out.println("您輸入的資料非法"); }else if(age< 18){ System.out.println("青少年時期"); }else if(age <35){ System.out.println("青壯年"); }else if(age <60) { System.out.println("中年時期"); }else if(age > 120){ System.out.println("老年時期"); }else { System.out.println("你是要成仙"); } } } ---------- java ---------- 檢查結束 你可以看成人了 青壯年 輸出完成 (耗時 0 秒) - 正常終止
從鍵盤輸入測試
/* 如何從鍵盤獲取不同的值;使用Scanner類 實現步驟 1.導包:import java.util.Scanner; 2.Scanner 的例項化 3.呼叫方法獲取指定型別變數 */ import java.util.Scanner; class ScannerTest { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num = scan.nextInt(); System.out.println(num); } }
示例2
/* 如何從鍵盤獲取不同的值;使用Scanner類 實現步驟 1.導包:import java.util.Scanner; 2.Scanner 的例項化 3.呼叫方法獲取指定型別變數 */ import java.util.Scanner; class ScannerTest { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("請輸入姓名:"); String name = scan.next(); System.out.println(name); System.out.println("請輸入年齡:"); int age = scan.nextInt(); System.out.println(age); System.out.println("請輸入你的體重:"); double weight = scan.nextDouble(); System.out.println(weight); System.out.println("性別:"); boolean islove =scan.nextBoolean(); System.out.println(islove); System.out.println("男/女"); String gender = scan.next(); char genderChar = gender.charAt(0); System.out.println(genderChar); } }
示例2
/* 如何從鍵盤獲取不同的值;使用Scanner類 實現步驟 1.導包:import java.util.Scanner; 2.Scanner 的例項化 3.呼叫方法獲取指定型別變數 */ import java.util.Scanner; class ScannerTest { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("請輸入成績:"); int score = scan.nextInt(); if (score == 100) { System.out.println("獎勵一輛車"); } else if (score > 80 && score <= 99 ) { System.out.println("獎勵一部手機"); } else if (score >= 60 && score <80) { System.out.println("獎勵一臺遊戲機"); } else { System.out.println("哪裡也不能去在家學習"); } } }
測試
比較三個數大小
import java.util.Scanner; class IfTest2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("請輸入第一個整數:"); int num1 = scan.nextInt(); System.out.println("請輸入第二個整數:"); int num2 = scan.nextInt(); System.out.println("請輸入第三個整數:"); int num3 = scan.nextInt(); if (num1 >= num2) { if (num3 >= num1) { System.out.println(num2 + "," + num1 + "," + num3); } else if (num3 <= num2) { System.out.println(num3 + "," + num2 + ","+ num1); } else { System.out.println(num2 + "," + num3 + "," + num1); } }else { if (num3> num2) { System.out.println(num1 + ","+ num2 + "," +num3); } else if (num3 <= num1) { System.out.println(num3 + "," + num1 + ","+num2); } else { System.out.println(num1 + "," +num3 + "," + num2); } } } }
測試
示例2
import java.util.Scanner; class IfEtst { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("請輸入身高:"); int height = scan.nextInt(); System.out.println("請輸入你的財富:(千萬)"); double wealth = scan.nextDouble(); System.out.println("請輸入你是否帥:(true/false)"); boolean isHandsome = scan.nextBoolean(); if (height >= 180 && wealth >=1 && isHandsome) { System.out.println("我非他不嫁!"); }else if (height >= 180 || wealth >=1 || isHandsome) { System.out.println("嫁了吧,比上不足,比下有餘!"); }else { System.out.println("不嫁!"); } } }
測試
示例2
import java.util.Scanner; class IfEtst { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("請輸入身高:"); int height = scan.nextInt(); System.out.println("請輸入你的財富:(千萬)"); double wealth = scan.nextDouble(); System.out.println("請輸入你是否帥:(是/否)"); String isHandsome = scan.next(); if (height >= 180 && wealth >=1 && isHandsome.equals("是"))//#判斷變數裡的值是否與括號裡的值相同 { System.out.println("我非他不嫁!"); }else if (height >= 180 || wealth >=1 || isHandsome.equals("是")) { System.out.println("嫁了吧,比上不足,比下有餘!"); }else { System.out.println("不嫁!"); } } }
測試