開始使用免費開始

總報酬計算器

公司需要每位員工的總報酬數值,也就是把薪資與獎金加總。因為這個計算需要在同一列同時使用兩個不同欄位的值,你需要逐列走訪,而不是只轉換單一欄位。

已匯入 Tablesaw 函式庫,且 employees.csv 包含 SalaryBonus 欄位。

本練習屬於課程

Java 中的資料匯入

檢視課程

練習說明

  • 逐列走訪,每列同時存取薪資與獎金的數值。
  • 將兩個數值相加,計算總報酬。
  • totalComp 欄位新增到 employees 表格。

動手互動練習

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

public class TotalCompensation {
    public static void main(String[] args) {
        Table employees = Table.read().csv("employees.csv");
        
        // Iterate through each row
        DoubleColumn totalComp = DoubleColumn.create("TotalCompensation");
        employees.____(row -> {
            double salary = row.getInt("Salary");
            double bonus = row.getInt("Bonus");
            // Sum salary and bonus
            totalComp.append(____ + ____);
        });
        
        // Add totalComp column
        employees.addColumns(____);
        
        System.out.println("Total Compensation Analysis:");
        System.out.println(employees.first(5));
    }
}
編輯並執行程式碼