1. 程式人生 > >《Java編程思想》筆記 第四章 控制執行流程

《Java編程思想》筆記 第四章 控制執行流程

ascii cas div 當前 也不會 system 包括 進入 ont

1.true和false

1.1 if--else if--else, while, do--while 都使用條件表達式的真假來決定執行路徑。

1.2 Java不允許數字作為真假判斷,C和C++可以非0即真。

2.Math.random()

  • 產生一個double值 [ 0, 1 ) 包括0,不包括1.

3.foreach語法

  • 如for(float x: f){ } f是一個數組,返回一個數組的方法都可以用foreach

4.return

  • return使當前方法退出,並返回值。
  • 除構造器外任何方法都有返回值類型 如void fun(), String fun(), void fun(),不需要寫return,默認有return, 其他方法 必須要有return 值(或表達式產生一個值)

5.break和continue

  • break終止當前所屬層循環,並退出循環,continue終止當前所屬層循環,並進入下一次循環。

5.1 標簽: 要跳出嵌套循環時使用

continue leab;//跳到標簽所在的循環開始再循環,註意內層循環中i不會增加,// break leab;跳出標簽所在的循環,不再循環,內層i也不會增加
        leab:
        // 標簽與叠代體之間不能加入其他代碼
        for (j = 0; j < 5; j++) {
            System.out.println("外");
            for (; i < 10; i++) {
                
if (i == 6) { continue leab; // break leab; } System.out.println("nei"); } }

6 . switch (值)

switch(i){ //
        case 1: System.out.println(1);break;
        case 2: System.out.println(1);
        
case ‘a‘: System.out.println(97);break; // char ‘a‘ 自動轉為ASCII值 }

《Java編程思想》筆記 第四章 控制執行流程