运行总数而不是个人总数?

时间:2013-11-29 19:18:12

标签: java

我需要开发一个工资计算器应用程序,该应用程序计算每周多个商店单位的员工总成本,然后将每个单位的人员成本打印到屏幕上。但是,该程序目前只在屏幕上打印每个单元的累计总数(即,如果第一单元的人员成本为450英镑,而第二单元的人员成本为0英镑,则该计划仍将第二单元的人员成本打印为£450而不是£0)。我有什么想法可以改变我的代码来解决这个问题吗?

import java.util.*;
import java.io.*;

public class Task3Second {

static Scanner console = new Scanner(System.in);    

public static void main(String[] args) throws FileNotFoundException 
{
    Scanner inFile = new Scanner(new FileReader("M:\\4001COMP-CW1-Task3-Infile.txt"));
    PrintWriter outFile = new PrintWriter("results.txt");

    String ShopUnit;
    int NoOfStaff;
    int HoursWorkedPerWeek;
    double HourlyRate;
    double TotalStaffCost = 0;
    int count = 0;
    double RecommendedMaximumStaffCost;

    System.out.println("Please enter the recommended maximum staff cost.");
    RecommendedMaximumStaffCost = console.nextDouble();
    System.out.println("The recommended maximum staff cost is £" + RecommendedMaximumStaffCost);
    System.out.println();
    outFile.println("The recommended maximum staff cost is £" + RecommendedMaximumStaffCost);

    while(inFile.hasNext()){
        count++;
        ShopUnit = inFile.nextLine();
    if(ShopUnit.startsWith("Unit")){
        NoOfStaff = inFile.nextInt();
        for(count = 0; count < NoOfStaff; count++){
            HoursWorkedPerWeek = inFile.nextInt();
            HourlyRate = inFile.nextDouble();
            TotalStaffCost = TotalStaffCost + (HoursWorkedPerWeek * HourlyRate);
        }
        if (TotalStaffCost < RecommendedMaximumStaffCost)
        {System.out.println("The total staff cost of " + ShopUnit + " is £" + TotalStaffCost + ", which is less than the recommended staff cost.");
        PrintWriter outFile1 = new PrintWriter("results.txt");
        outFile1.println("The total staff cost of " + ShopUnit + " is £" + TotalStaffCost);
        }else if (TotalStaffCost == RecommendedMaximumStaffCost)
        {System.out.println("The total staff cost of " + ShopUnit + " is £" + TotalStaffCost + ", which is equal to the recommended staff cost.");  
        }else {System.out.println("The total staff cost of " + ShopUnit + " is £" + TotalStaffCost + ", which is more than the recommended staff cost.");}

        }
    }

    System.out.println();
    System.out.println("The total staff cost of all the shopping units is £" + TotalStaffCost);
    outFile.println("The total staff cost of all the shopping units is £" + TotalStaffCost);

    inFile.close();
    outFile.close();

}
}

1 个答案:

答案 0 :(得分:0)

以下是计算运行总计和单位总数的方法:

  1. 创建一个变量来保存运行总计;也许把它命名为“runningTotal”。将其初始化为零。
  2. 创建一个变量来保存单位总数;也许把它命名为“unitTotal”。
  3. 所有单位的
  4. 执行以下操作:
    1. 将单位总数重置为零。
    2. 计算当前单位的总数。
    3. 显示当前单位的总数。这包括将单位总数存储在集合中以供稍后显示的选项。
    4. 将单位总数添加到运行总计中。
  5. 显示运行总计。
  6. 在您的代码中,您有一个总计变量,即运行总计。你需要两个变量;一个用于总计,一个用于总计。

    如果“单位”令人困惑,请将其读作“商店”。