Java流程控制(Scanner,順序,選擇,switch以及回圈結構)

2020-08-11 16:43:45

Scanner

        Scanner s=new Scanner(System.in);
        System.out.println("使用next方式接受");
        //判斷使用者有沒有輸入字串
        if(s.hasNext())
        {
            String s1=s.next();
            System.out.println("輸出內容爲:" +s1);
        }
        s.close();
//使用next方式接受
//hello word!
//輸出內容爲:hello

        Scanner s=new Scanner(System.in);
        System.out.println("使用nextLine方式接受");
        //判斷使用者是否還有輸入字串
        if(s.hasNextLine())
        {
            String s1=s.nextLine();
            System.out.println("輸出內容爲:" +s1);
        }
        s.close();//關閉Scanner,凡是屬於IO流的類如果不關閉會一直佔用資源
/**
使用next方式接受
hello word!
輸出內容爲:hello word!
*/

next()

  • 一定要讀取到有效字元後纔可以結束輸入
  • 對輸入有效字元之前遇到空白,next()方法會自動去掉
  • 只有輸入有效字元後纔將其後面輸入的空白作爲分隔符或者結束符
  • next()不能得到帶有空格的字串

next Line()

  • 以enter爲結束符,也就是說next Line()方法返回的是輸入回車之前的所有字元
  • 可以獲得空白
        Scanner s=new Scanner(System.in);
        //和
        double sum=0;
        //計算輸入了多少個數字
        int m=0;
        //通過回圈判斷是否還有輸入,並在裏面對每一次進行求和和統計
        while (s.hasNextDouble()){
            double x=s.nextDouble();
            m = m+1;
            sum = sum + x;
            System.out.println("你輸入了第"+m+"個數,當前和爲"+sum);
        }
        System.out.println(m+"個數和爲:"+sum);
        System.out.println(m+"個數平均值爲:"+(sum/m));

        s.close();
/*
10
你輸入了第1個數,當前和爲10.0
20
你輸入了第2個數,當前和爲30.0
30
你輸入了第3個數,當前和爲60.0
15
你輸入了第4個數,當前和爲75.0
45
你輸入了第5個數,當前和爲120.0
50
你輸入了第6個數,當前和爲170.0
x
6個數和爲:170.0
6個數平均值爲:28.333333333333332
*/

順序結構

從上到下依次進行

選擇結構

if單選擇結構

        Scanner s=new Scanner(System.in);
        System.out.println("請輸入一個內容:");
        String s1=s.nextLine();
        if (s1.equals("1231")){
            System.out.println(s1);
        }
            System.out.println("end");
        s.close();
/*
12            1231
end           1231
              end

if雙選擇結構

        Scanner s=new Scanner(System.in);
        System.out.println("請輸入成績:");
        int s1=s.nextInt();
        if (s1 >= 60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }

        s.close();
/*
50            60
不及格         及格

if 多選擇結構

        Scanner s=new Scanner(System.in);
        System.out.println("請輸入成績:");
        double s1=s.nextDouble();
        if (s1 == 100){
            System.out.println("恭喜滿分");
        }else if (s1 < 100 && s1 >=90){
            System.out.println("A");
        }else if (s1 < 90 && s1 >=70){
            System.out.println("B");
        }else if (s1 < 70 && s1 >=60){
            System.out.println("C");
        }else if (s1 < 60 && s1 >=0){
            System.out.println("不及格");
        }else {
            System.out.println("成績不合法" +
                    "");
        }

        s.close();
//其中有一個爲true,則跳過執行

if巢狀結構

if(布爾值){
   if(布爾值){
      if(布爾值){
         ..
         ..
     }
   }
}

switch 多選擇結構

        char grade = 'A';

        switch (grade){
            case 'A':
                System.out.println("優");
                break;//一定要加上,否則case穿透,會一直往下執行
            case 'B':
                System.out.println("良");
                break;
            case 'C':
                System.out.println("中");
                break;
            case 'D':
                System.out.println("差");
                break;
            default:
                System.out.println("未知等級");
        }

回圈結構

while回圈

回圈結構:
while(布爾表達式){
    回圈內容
}
  • 只要布爾表達式爲true ,回圈就一直執行下去
  • 我們大多數情況是會讓回圈停止下來,我們需要一個表達式失效的方式來結束回圈
  • 少部分情況需要一直回圈,比如伺服器的請求響應監聽等
  • 回圈條件一直爲true就會造成死回圈,我們正常業務程式設計中應該儘量避免死回圈。否則會影響程式效能,造成程式卡死崩潰
       //輸出1-100
        int i = 0;
        while (i < 100){
            i++;
            System.out.println(i);
        }
       //計算1+2+3+4+..+100
        int i = 0;
        int sum = 0;
        while (i < 100){
            i++;
            sum = sum + i;

        }System.out.println(sum);
    }

do…while回圈

回圈結構:
do{
   回圈語句
}while(布爾表達式)

while 與 do…while回圈的區別

  • while先判斷後執行, do…while是先執行後判斷!
  • do…while保證回圈體至少被執行一次
       //計算1+2+3+4+..+100
        int i = 0;
        int sum = 0;
        do{
            i++;
            sum = sum + i;

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

For回圈

for回圈語句是支援迭代的一種通用結構,是最有效,最靈活的回圈結構

回圈結構:
for(初始化;布爾表達式;更新){
     程式碼語句
}
  //輸出1-100
       int a = 0;
       while (a<=100){
           System.out.println(a);
           a+=2;
       }
        System.out.println("while 回圈結束");
        for (int i=0;i<=100;i++){
            System.out.println(i);
        }
        System.out.println("for回圈結束");

        /**
         * 關於for回圈有一下幾點說明
         * 1.最先執行初始化步驟,可以宣告一種型別,但可初始化一個或多個回圈控制變量,也可以是空語句
         * 2.檢測布爾表達式的值,如果爲true,回圈體被執行。如果爲false,回圈體被終止,開始執行回圈體後面的語句
         * 3.執行一次回圈體後更新回圈控制變量(迭代因子控制回圈變數的增減)
         * 4.再次檢測布爾表達式,回圈執行上面的過程
         */

        //死回圈
        for ( ; ;  ) {

        }     
        //計算1+2+3+..+100的和
        int sum = 0;
        for (int i=0;i<=100;i++){
            sum = sum + i;

        } System.out.println(sum);


       //計算0-100的奇數和偶數的和
        int addSum = 0;
        int evenSum = 0;
        for (int i = 0; i < 100; i++) {
            if((i % 2) != 0){
                addSum+=i;
            }else {
                evenSum+=i;
            }
        }
        System.out.println("奇數的和"+addSum);
        System.out.println("偶數的和"+evenSum);
    }


        //用while或for回圈輸出1-1000之間能被5整除的數,並且每行輸出3個
        int i = 0;
        while (i < 1000) {
            if ((i % 5) == 0) {
                System.out.print(i+"\t");
            }
            if ((i % (5 * 3)) == 0){
                System.out.println( );
            }
            i++;
        }

        for (int i1 = 0; i1 < 1000; i1++) {
            if((i1 % 5) == 0){
                System.out.print(i1+"\t");
            }
            if ((i1 % (5 * 3)) == 0){
                System.out.println( );
            }
        }/////println    輸出完會換行
        //////print    輸出完不會換行



        /**
         * 1. 我們先列印第一列
         * 2. 把固定的1再用回圈包起來
         * 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回圈

主要是用來遍歷陣列和集合

回圈結構:
for(宣告語句:表達式)
{
   程式碼語句
}
  • 宣告語句:宣告新的區域性變數,該變數的型別必須和陣列元素的型別匹配。其作用域限定在回圈語句塊,其值與此時陣列的值相等
  • 表達式:表達式是要存取的陣列名,或者是返回值爲陣列的方法
        //遍歷陣列的元素
         int [ ] num= {10,20,30,40,50};//定義了一個數組
        for (int x : num){
            System.out.print(x+"\t");
        }
        for(int i = 0; i<5;i++){
            System.out.println(num[i]);
        }

break continue

break

強制退出回圈(回圈體之後的語句正常執行)

        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
            if(i == 10){
                break;
            }
        }
//   12345678910

continue

用於終止某次回圈過程(跳過某次回圈)

        int i = 0;
        while (i<50){
            i++;
            if((i %10)==0){
                System.out.println(" ");
                continue;
            }System.out.print(i);
        }
/**
 * 123456789 
 * 111213141516171819 
 * 212223242526272829 
 * 313233343536373839 
 * 414243444546474849 
 */

列印三角形

//列印三角形
for (int i = 1; i <= 5; i++) {
    for (int j = 5;j >= i;j--){
        System.out.print(" ");
    }
    for (int j = 1;j <= i; j++){
        System.out.print("*");
    }
    for (int j = 1;j<i;j++){
        System.out.print("*");
    }

    System.out.println(" ");
}