Java學習---流程控制

流程控制

1.關係運算符

+、-、*、/、>、<、=

int a= 10 , b = 5;

       if (a>3) {

           System.out.println("a的值大於3 ");

       }

       if (b<3) {

           System.out.println("b的值小於3");

    }

2.邏輯運行

&& ||

boolean f=(age>4&&dj>=40||age==6&&dj>30);

3.通過控制檯打印矩形

       Scanner input = new Scanner(System.in);

       System.out.println("請輸入矩形的邊長:");

       int bc = input.nextInt();

       input.close();

       for (int i = 0; i < bc;i++) {

           System.out.print("*");

       }

       System.out.println();

       for (int i = 0; i < bc-2;i++) {

           System.out.print("*");

           for (int j = 0; j < bc-2;j++) {

              System.out.print(");

           }

           System.out.println("*");

       }

       for (int i = 0; i < bc;i++) {

           System.out.print("*");

    }

4.條件運算

表達式1?表達式2:表達式3

boolean t = (1+1==2)?true:false;

5.If語句

是最常見的選擇語句

int a= 10 , b = 5;

       if (a>3) {

           System.out.println("a的值大於3 ");

       }

       if (b<3) {

           System.out.println("b的值小於3");

    }

6.If-else語句

    是最常見的選擇語句

     比if多了一個選擇情況的執行

 int x=5;

       if(x==1)

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

       elseif(x==2)

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

       elseif(x==3)

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

       else

        System.out.println("x>3");

7.Switch語句

If語句每次只能實現兩天分支 如果要實現多重選擇 就需要使用switch語句

System.out.println("請輸入運算符:");

       charoper=input.next().charAt(0);

       switch (oper) {

       case'+':

           System.out.println(a+"+"+b+"="+(a+b));

           break;

       case'-':

           System.out.println(a+"-"+b+"="+(a-b));

           break;

       case'*':

           System.out.println(a+"*"+b+"="+(a*b));

           break;

       case'/':

           System.out.println(a+"/"+b+"="+(a/b));

           break;

       default:

           System.out.println("未知的操作!");

           break;

    }

8.循環結構

是程序中的另一種重要結構 它和順序結構、選擇結構共同作爲各種複雜程序的基本構造部件.

特點:是在給定的條件成立時,反覆執行某個程序段.

a. while循環

while是最簡單的循環語句 它有條件的將內嵌語句執行0遍或若干遍

//循環計算1+2+3+..+10的結果

int i =1,sum=0;

       while (i<=10) {

           sum+=i;

           i++;

       }

     System.out.println("1+2+3+..+10="+sum);

b. do-while循環

是while循環的變式 它會先執行一次循環體 然後再判斷循環條件

int i = 1, sum = 0;

       do {

           sum+= i;

           i++;

       }while (i <= 10);

     System.out.println("1+2+3+..+10=" + sum);

 

c. for循環

是Java語音中使用頻率最高、功能最強大的循環語句

在事先知道循環次數的情況下 使用for語句是比較簡單的

int i = 1, sum = 0;

       for (i = 1; i <= 10;i++) {

           sum+= i;

       }

     System.out.println("1+2+3+..+10=" + sum);

d. 循環嵌套

當循環語句內再次出現循環語句 就稱爲循環嵌套

        int i, j;

       for (i = 1; i <= 9;i++) {

           for (j = 1; j <= 9;j++) {

              System.out.print(j+ "*" + i + "=" + (i * j) + "\t");

           }

           System.out.print("\n");

       }

9.跳轉語句

a. break語句

也稱爲中斷語句 它通常用來在適當的時候推出某個循環

int i;

       for ( i = 1; i <=10;i++) {

           if (i%3==0) {

              break;

           }

           System.out.println("i+"+i);

       }

     System.out.println("循環中斷:i="+i);

b. continue語句

是結束本次循環 在continue語句出跳轉到循環的起點繼續進行循環

int i;

       for ( i = 1; i <=10;i++) {

           if (i%3==0) {

              continue;

           }

           System.out.println("i+"+i);

       }

     System.out.println("循環中斷:i="+i);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章