建立工作流程

Java 中級

Jim White

Java Developer

奇數乘二,偶數除二

class DoubleOrHalve {
  public static void main(String[] args) {
    int[] numbers = {23, 78, 2263, 552};
    int[] newNumbers = doubleOddHalveEven(numbers); // Value is 46, 39, 4526, and 276
  }
  static int[] doubleOddHalveEven(int[] arr) {
    for (int i = 0; i < arr.length; i++) { // Loop through the array
      if (arr[i] % 2 == 0) { 
        arr[i] /= 2; // Halve even numbers
      } else {
        arr[i] *= 2; // Double odd numbers
      }
    }
    return arr;
    }
}

Java 中級

方法鏈結(Method chaining)

  • 在同一個值上連續呼叫方法
    • 例如:variable.method1().method2()
class CheckStrings {
  public static void main(String[] args) {
    String message = "Hello World!";
    String input = "hello world!";

    // Convert message to lower case, then compare with input 
    System.out.println(message.toLowerCase().equals(input));
    }   
}
true
Java 中級

結構化

  • 將程式碼拆成清楚步驟
  • 使用有意義的名稱
  • 把相關邏輯放在一起

➡ 讓程式碼更易讀、易改、好維護

人走上樓梯

Java 中級

一起來練習吧!

Java 中級

Preparing Video For Download...