開始使用免費開始

新價格

政府剛對你的商品開徵新稅。你想一次更新庫存中的所有價格,將每個價格加上其一半的金額。

本練習屬於課程

Java 中級

檢視課程

練習說明

  • 為你的函式提供正確的型別
  • 回傳參數的 1.5 倍。
  • 使用合適型態的 for 迴圈。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

class PriceCalculator {
  // Give the correct type to your function
  static ____ oneAndAHalf(double a) {
    // Use the proper operations to get the result
    return ____;
  }

  public static void main(String[] args) {
    double[] numbers = {76, 38.3, 10, 42, 98.5, 84, 50, 72.2, 98, 96};

    // Use the congruous type of for loop
    for (____) {
      numbers[i] = oneAndAHalf(numbers[i]);
      System.out.println(numbers[i]);
    }
  }
}
編輯並執行程式碼