Day6 三種結構 順序選擇循環!


順序結構

從上到下依次執行,它是任何算法都離不開的一種基本算法結構。

package com.xiaoming.struct;

public class ShunXuDemo {
   public static void main(String[] args) {
       System.out.println("hello1");
       System.out.println("hello2");
       System.out.println("hello3");
       System.out.println("hello4");
       System.out.println("hello5");
  }

}

 

選擇結構

if單選擇結構

判斷一個東西是否可行,用if語句來表示。

語句:

if(布爾表達式){

//如果布爾表達式為true將執行的語句

}

package com.xiaoming.struct;

import java.util.Scanner;

public class IfDemo01 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);

       System.out.println("請輸入內容:");
       String s = scanner.nextLine();
       // equals:判斷字符串是否相等
       if (s.equals("Hello")){
           System.out.println(s);

      }

       System.out.println("End");

       scanner.close();
  }
}

 

if雙選擇結構

語句:

if(布爾表達式){

//如果布爾表達式的值為true

}else{

//如果布爾表達式的值為true

}

package com.xiaoming.struct;

import java.util.Scanner;

public class IfDemo02 {
   public static void main(String[] args) {
       //考試分數大於60及格,小於60不及格。

       Scanner scanner = new Scanner(System.in);

       System.out.println("請輸入成績:");

       int score = scanner.nextInt();

       if (score>=60){
           System.out.println("及格");
      }else{
           System.out.println("不及格");
      }



       scanner.close();
  }
}

if多選擇結構

if(布爾表達式1){

//如果布爾表達式1的值為true執行代碼

}else if{布爾表達式2){

//如果布爾表達式2的值為true執行代碼

}else if(布爾表達式3){

//如果布爾表達式3的值為true執行代碼

}else {

//如果以上布爾表達式都不為true執行代碼

}

package com.xiaoming.struct;

import java.util.Scanner;

public class IfDemo03<score> {
   public static void main(String[] args) {

       Scanner scanner = new Scanner(System.in);
       /* if 語句至多有一個else語句,else語句在所有else if 語句之后。
          if 語句可以有若干個else if 語句,它們必須在else語句之前。
          一旦其中一個else if 語句檢測為true,其他的else if 以及else 語句都將跳過執行
        */

       System.out.println("請輸入成績:");

       int score = scanner.nextInt();

       if (score == 100) {
           System.out.println("恭喜滿分");
      } else if (score< 100 && score >= 90){
       System.out.println("A級");
      } else if (score<90 && score >=80){
           System.out.println("B級");
      }else if (score<80 && score >=70){
           System.out.println("C級");
      }else if (score<70 && score >=60){
           System.out.println("D級");
      }else if (score<60 && score >=0){
           System.out.println("不及格");
      }else {
           System.out.println("成績不合法");
      }


       scanner.close();
  }
}

嵌套的if語句

語句:

if(布爾表達式1){

///如果布爾表達式1的值為true執行代碼

if(布爾表達式2){

///如果布爾表達式2的值為true執行代碼

}

}

 

Switch多選擇結構

多選擇還有一個實現方式就是switch case語句。

switch case 語句判斷判斷一個變量與一系列值中某個值是否相等,每個值稱為一個分支。

 

語句:

switch(expression){
   case value:
       //語句
       break;//可選
   case value:
       //語句
       break;//可選
       //你可以有任意數量的case語句
       default : //可選
       //語句
}

 

 

Switch中的變量類型可以是:

  • byte short int char String(Java SE7開始)

  • case標簽必須為字符串常量或字面量

 

package com.xiaoming.struct;

public class SwitchDemo01 {
   public static void main(String[] args) {
       //case 穿透 //switch:匹配一個具體值
       char grade = 'C';

       switch(grade){
           case 'A':
               System.out.println("優秀");
               break; //可選
           case 'B':
               System.out.println("良好");
               break; //可選
           case 'C':
               System.out.println("及格");
               break; //可選
           case 'D':
               System.out.println("再接再厲");
               break; //可選
           case 'E':
               System.out.println("掛科");
               break; //可選
           default:
               System.out.println("未知等級");


      }
  }
}

注意:case穿透現象:

 

 

如果不加break case回一直執行下去輸出全部結果 這就是case穿透現象。

所以每用一個case語句要同時加上break

 

 

package com.xiaoming.struct;

public class SwitchDemo02 {
   public static void main(String[] args) {
       String name = "xiaoming";
       // JDK7新特性,表達式結果可以是字符串!!!
       // 字符串的本質還是數字

       //反編譯 jva---class (字節碼文件)------反編譯(Idea)


       switch (name){
           case"xiaoming":
               System.out.println("xiaoming");
               break;
           case"Java":
               System.out.println("Java");
               break;
           default:
               System.out.println("弄啥嘞!");

      }
  }
}

 

 

通過將.class文件拖到.java里反編譯發現 switch輸入字符串變量是是利用比較哈希值相不相同

 

循環結構

  • while循環

  • do ...while 循環

  • for循環

  • 在java5中還引入了一種主要用於數組的增強型for循環。

 

while循環:

while(布爾表達式){

//循環內容

}

  • 只要布爾表達式為true,循環就會一直執行下去。

  • 我們大多數情況下是會讓循環停止下來,我們需要一個讓表達式失效的方式來結束循環。

  • 少部分情況需要循環一直執行下去,比如服務器的請求響應監聽等。

  • 循環條件一直為true就會造成無限循環[死循環],我們正常的業務編程中應該盡量避免死循環。會影響程序性能或者造成程序卡死崩潰!

  • 思考:計算1+2+3+...+100=?

 

 

package com.xiaoming.struct;

public class WhileDemo01 {
   public static void main(String[] args) {

       //輸出1~100

       int i = 0;

       while(i<100){
           i++;
           System.out.println(i);
      }
  }
}
package com.xiaoming.struct;

public class WhileDemo02 {
   public static void main(String[] args) {
       //死循環
       while(true){
           // 等待客戶端鏈接
           // 定時檢查
           //...........

      }
  }
}

1+2+3+...+100=?

package com.xiaoming.struct;

public class WhileDemo03 {
   public static void main(String[] args) {
       //計算1+2+3+...+100=?

       //高斯的故事

       int i = 0;
       int sum = 0;

       while (i<=100){
           sum = sum + i;
           i++;
      }
       System.out.println(sum);
  }
}

do while 循環

  • 對於while語句而言,如果不滿足條件,則不能進入循環。但有時候我們需要即使不滿足條件,也至少執行一次。

  • do...while 循環和while循環相似,不同的是,do...while循環至少會執行一次。

    do{

    //代碼語句

    }while(布爾表達式);

    while和do while的區別

    while先判斷后執行。do while 先執行后判斷

    do...while總是保證循環體至少被執行一次!這是主要差別

     

    package com.xiaoming.struct;

    public class DoWhileDemo01 {
       public static void main(String[] args) {

           int i = 0;
           int sum = 0;

           do {
               sum =sum + i;
               i++;
          }while (i<=100);

           System.out.println(sum);
      }
    }

     

    package com.xiaoming.struct;

    public class DoWhileDemo02 {
       public static void main(String[] args) {
           int a = 0;
           while(a<0){
               System.out.println(a);
               a++;
          }
           System.out.println("========================================");
           do{
               System.out.println(a);
               a++;
          }while(a<0);  //0 先執行后判斷;
      }
    }

     

    For循環:

  • 雖然所有循環結構都可以用while或者do...while表示,但Java提供了另一種語句———for循環,使一些循環結構變得更加簡單。

  • for循環語句是支持迭代的一種通用結構,是最有效、最靈活的循環結構。

  • for循環執行的次數是在執行前就確定的。

    語句:

    for(初始化;布爾表達式;更新){

    //代碼語句

    }

    package com.xiaoming.struct;

    public class ForDemo01 {
       public static void main(String[] args) {
           int a = 1;  //初始化條件

           while (a<=100){//條件判斷
               System.out.println(a); //循環體
               a+=2; //迭代

          }
           System.out.println("while循環結束!");


           //初始化值 // 條件判斷 // 迭代
           for(int i =1;i<=100;i++){
               System.out.println(i);
          }
           System.out.println("for循環結束!");

           /*
           關於for循環以下幾點說明:

           最先執行初始化步驟。可以聲明一種類型,但可初始化一個或多個循環控制變量,也可以是空語句。
           然后,檢測布爾表達式的值。如果為true,循環體被執行。如果為false,循環終止,開始執行循環體后面的語句。
           執行一次循環后,更新循環控制變量(迭代因子控制循環變量的增減)。
           再次檢測布爾表達式。循環執行上面的過程。
            */
           // 死循環
           for (; ; ) {
          }
          }
      }

    快捷寫法:100.for 回車會生成for (int i = 0; i < 100; i++) {

     

練習1:計算0到100之間的奇數和偶數的和

package com.xiaoming.struct;

public class ForDemo02 {
   public static void main(String[] args) {
       //練習1:計算0到100之間的奇數和偶數的和

       int oddSum = 0;
       int evenSum = 0;

       for (int i = 0; i <= 100; i++) {
           if (i % 2 != 0) { //奇數
               oddSum += i;
          } else { //偶數
               evenSum += i;
          }
      }

       System.out.println("奇數的和"+oddSum);
       System.out.println("偶數的和"+evenSum);

       System.out.println("========================================");


       int oddSum2=0;
       int evenSum2=0;
       int i2=0;

       while(i2<=100){
           i2++;
           if (i2%2!=0){
               oddSum2+=i2;
          }else{
               evenSum2+=i2;

               System.out.println("奇數的和"+oddSum2);
               System.out.println("偶數的和"+evenSum2);
          }
      }


  }

}

 

練習2:用while 或for循環輸出1~1000之間能被5整除,並且每行輸出3個

package com.xiaoming.struct;

public class ForDemo03 {
   public static void main(String[] args) {
       //練習2: 用while或for循環輸出1~1000之間能被5整除的數,並且每行輸出3個

       for (int i = 0; i < 1000; i++) {
           if(i%5==0){
               System.out.print(i+"\t");
          }
           if(i%(5*3)==0) { //每行
               System.out.println();
               // System.out.print("\n");
          }
           
      }

       //println 輸出完會換行
       //print   輸出完不會換行


  }
}

 

這里注意print 是輸出完不會換行

 

練習3:打印99乘法表

package com.xiaoming.struct;

public class ForDemo04 {

   //九九乘法表
   /* 1*1=1
      1*2=2 2*2=4
      1*3=3 2*3=6   3*3=9
      1*4=4 2*4=8   3*4=12 4*4=16
      1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
      1*6=6 2*6=12 3*6=18 4*6=24 5*6=30   6*6=36
      1*7=7 2*7=14 3*7=21 4*7=28 5*7=35   6*7=42 7*7=49
      1*8=8 2*8=16 3*8=24 4*8=32 5*8=40   6*8=48 7*8=56 8*8=64
      1*9=9 2*9=18 3*9=27 4*9=36 5*9=45   6*9=54 7*9=63 8*9=72 9*9=81
    */
   public static void main(String[] args) {
       //1.打印第一列
       //2.我們把固定的第一列再用一個循環包起來
       //3.去掉重復項,i <= j;
       //4. 調整樣式
       for (int j = 1; j <= 9; j++) {
           for (int i = 1; i <= j; i++) {
               System.out.print(j+"*"+i+"="+(j*i)+ "\t");
          }
               System.out.println();

      }

  }
}

加強for循環

  • 數組重點使用

  • Java5引入了一種主要用於數組或集合的增強型for循環

語句:

for(聲明語句:表達式)

{

//代碼語句

}

聲明語句:聲明新的局部變量,該變量的類型必須和數組元素的類型匹配。其作用域限定在循環語句塊,其值與此時數組元素值相等。

表達式:表達式是要訪問的數組名,或者是返回值為數組的方法。

package com.xiaoming.struct;

public class ForDemo05 {
   public static void main(String[] args) {
       int [] numbers = {10,20,30,40,50}; // 定義了一個數組


       for(int i =0;i<5;i++){
           System.out.println(numbers[i]);
      }
       System.out.println("=======================================");
       //遍歷數組元素
       
       //增強for循環
       for (int x:numbers){
           System.out.println(x); /*10
                                    20
                                    30
                                    40
                                    50*/

      }
  }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM