java從入門到放棄--[1.3]流程控制

流程控制

  • switch-case

與 if 不同的是,switch-case 只能完成等值判斷,而無法完成判斷大小。

如果是判斷兩個值是否相等,可以使用 switch-case,如果比較兩個值的大小關係,則不能使用 switch-case。

switch 支持 int、short、byte、char、枚舉、String 類型,不支持 boolean 類型。

基本語法

switch(變量){
    case 值1:
        //業務代碼
    break;
    case 值2:
        //業務代碼
    breka;
    ...
    default:
        //業務代碼
    break;
}

case 判斷變量是否等於某個值,default 表示所有的 case 都不成立的情況下所執行的代碼。

  • 1 獎勵 2000

  • 2 獎勵 1000

  • 3 獎勵 500

  • 否則沒有獎勵

public static void main(String[] args) {
  int placing = 1;
  if(placing == 1) {
    System.out.println("獎勵2000元");
  }else {
    if(placing == 2) {
      System.out.println("獎勵1000元");
    }else {
      if(placing == 3) {
        System.out.println("獎勵500元");
      }else{
        System.out.println("沒有獎勵");
      }
    }
  }
​
  switch(placing) {
    case 1:
      System.out.println("獎勵2000元");
      break;
    case 2:
      System.out.println("獎勵1000元");
      break;
    case 3:
      System.out.println("獎勵500元");
      break;
    default:
      System.out.println("沒有獎勵");
      break;
  }
}

 

循環

for、while、do-while、foreach

循環四要素:

  • 初始化循環變量

  • 循環條件

  • 循環體

  • 更新循環變量

while

初始化循環變量
while(循環條件){
    循環體
    更新循環變量
}
//初始化循環變量
int num = 0;
//循環條件
while(num < 10) {
  //循環體
  System.out.println("Hello World");
  //更新循環變量
  num++;
}
int num = 0;
String flag = "y";
while(flag.equals("y")) {
  System.out.print("請輸入學生學號:");
  Scanner scanner = new Scanner(System.in);
  int id = scanner.nextInt();
  switch(id) {
    case 1:
      System.out.println("張三的成績是96");
      break;
    case 2:
      System.out.println("李四的成績是91");
      break;
    case 3:
      System.out.println("王五的成績是89");
      break;
    default:
      System.out.println("請輸入正確的學號");
      break;
  }
  System.out.print("是否繼續?y/n");
  flag = scanner.next();
}
System.out.println("感謝使用學生成績查詢系統");

 

do-while

//初始化循環變量
int num = 0;
do {
  //循環體
  System.out.println("Hello World");
  //更新循環變量
  num++;
}while(num<10);
//循環條件

 

Scanner scanner = new Scanner(System.in);
String result = "";
do {
  System.out.println("張三參加體能測試,跑1000米");
  System.out.print("是否合格?y/n");
  result = scanner.next();
}while(result.equals("n"));
System.out.println("合格,通過測試");

 

for

for(初始化循環變量;循環條件;更新循環變量){
        循環體
}
for(int num = 0;num < 10;num++) {
  System.out.println("Hello World");
}

 

while、do-while、for 3種循環的區別

  • 相同點:都遵循循環四要素,初始化循環變量、循環條件、循環體、更新循環變量。

  • 不同點:

    • while 和 do-while 適用於循環次數不確定的業務場景;for 適用於循環次數確定的場景。

    • while 和 for 都是先判斷循環條件,再執行循環體;do-while 先執行循環體,再判斷循環條件。

 

分別使用 while、do-while、for 循環輸出 10 以內的所有奇數。

//while循環
int num = 0;
while(num <= 10) {
  if(num%2!=0) {
    System.out.println(num);
  }
  num++;
}
//do-while循環
int num = 0;
do {
  if(num%2!=0) {
    System.out.println(num);
  }
  num++;
}while(num <= 10);
//for循環
for(int num = 0;num <= 10;num++) {
  if(num%2!=0) {
    System.out.println(num);
  }
}

 

for 循環只適用於循環次數確定的場景下(for 也可以適用於循環次數不確定的場景,只不過一般不會用這種方式進行開發),while 和 do-while 循環次數確定或者不確定都可以使用。

String result = "n";
for(;result.equals("n");) {
  System.out.println("張三參加體能測試,跑1000米");
  System.out.print("是否合格?y/n");
  result = scanner.next();
}
System.out.println("合格,通過測試");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章