我需要开发一个工资计算器应用程序,该应用程序计算每周多个商店单位的员工总成本,然后将每个单位的人员成本打印到屏幕上。但是,该程序目前只在屏幕上打印每个单元的累计总数(即,如果第一单元的人员成本为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();
}
}
答案 0 :(得分:0)
以下是计算运行总计和单位总数的方法:
在您的代码中,您有一个总计变量,即运行总计。你需要两个变量;一个用于总计,一个用于总计。
如果“单位”令人困惑,请将其读作“商店”。