对于我的课程,我们必须创建一个C#程序来读取文本文件,其中包含以下信息:员工ID,部门编号,小时工资和工作小时数。目标是显示每个部门的列表(总共7个),每个员工的总薪酬总和。每小时工资必须至少为10.00,工作小时数必须大于0。
我有一个程序来准确计算每个部门的员工人数;不是必需的,但对我有帮助。当我尝试使用类似的逻辑来计算每个部门的总工作时数和每小时总工资时,我遇到了麻烦。
到目前为止,这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Project3
{
class Program
{
const int DEPARTMENT = 7;
const int SIZE = 4;
static void Main(string[] args)
{
FileStream fstream = new FileStream("test.txt", FileMode.Open, FileAccess.Read);
StreamReader infile = new StreamReader(fstream);
string inputRecord = "";
string[] employee = new string[SIZE];
int department = 0;
int[] departmentCount = new int[DEPARTMENT];
double hourlySalary = 0.0;
double hoursWorked = 0.0;
double grossPay = hourlySalary * hoursWorked;
inputRecord = infile.ReadLine();
while (inputRecord != null)
{
employee = inputRecord.Split(',');
if (int.TryParse(employee[1], out department) && department > 0 && department <= DEPARTMENT)
departmentCount[department - 1] = departmentCount[department - 1] + 1;
else
Console.WriteLine(inputRecord);
if (double.TryParse(employee[2], out hourlySalary) && hourlySalary >= 10.0)
hourlySalary = hourlySalary + hourlySalary;
else
Console.WriteLine(inputRecord);
if (double.TryParse(employee[3], out hoursWorked) && hoursWorked > 0)
hoursWorked = hoursWorked + hoursWorked;
else
Console.WriteLine(inputRecord);
grossPay = hourlySalary * hoursWorked;
inputRecord = infile.ReadLine();
}//end while
showOutput(departmentCount, hourlySalary, hoursWorked, grossPay);
}//end main
static void showOutput(int[] departmentCount, double hourlySalary, double hoursWorked, double grossPay)
{
Console.WriteLine("Grosspay for each department");
Console.WriteLine("Department\t Employees");
for (int i = 0; i < departmentCount.Length; i++)
{
if (departmentCount[i] != 0)
{
Console.WriteLine("{0}\t\t {1}\t\t {2}\t\t {3}\t\t {4}", i + 1, departmentCount[i], hourlySalary, hoursWorked, grossPay);
}//end if
}//end for
}//end showOutput
}
}
以下是我用来测试程序的数据:
EID500,2,45,50
EID558,1,82,43
EID244,3,17,46
EID96,4,71,16
EID420,7,55,22
EID528,5,14,49
EID469,6,26,21
EID983,6,72,40
EID968,4,36,40
EID811,4,26,47
EID937,4,76,3
EID319,1,29,17
EID962,2,23,37
EID239,2,83,49
EID112,5,43,42
EID840,4,75,30
EID621,3,81,32
EID192,1,50,52
EID732,5,47,6
EID932,1,54,24
EID634,7,44,45
EID592,1,48,43
EID745,4,60,14
EID169,4,95,29
EID705,2,47,53
EID273,5,17,57
EID772,2,89,31
EID820,6,90,6
EID82,7,16,56
EID66,7,91,33
EID993,1,80,44
EID455,2,73,16
EID758,7,44,41
EID600,5,21,57
EID818,4,98,19
EID191,7,91,2
EID567,4,23,12
EID400,1,47,44
EID329,7,91,13
EID900,3,54,33
EID562,5,78,53
EID340,2,54,29
EID838,1,20,38
EID981,5,42,6
EID159,6,24,59
EID65,4,86,44
EID515,5,42,44
EID694,7,14,28
EID941,4,78,34
EID136,1,70,22
数组是我可以使用的最先进的函数,因为我们还没有学习我在这里阅读其他几篇文章的List项目。 我是否必须为每小时工资和工作时间创建数组?
非常感谢任何帮助。
-Henry
答案 0 :(得分:0)
不,你不需要为小时工资使用数组,但你需要有另一个变量。不要误会我的意思 - 也许阵列会更清洁。请考虑以下代码段:
if (double.TryParse(employee[2], out hourlySalary) && hourlySalary >= 10.0)
hourlySalary = hourlySalary + hourlySalary;
您需要有一个工资变量用于总计,另一个用于当前记录输入记录。我建议你将循环外定义的hourlySalary重命名为totalHourlySalary。然后在循环中定义另一个。
double hourlySalary
if (double.TryParse(employee[2], out hourlySalary) && hourlySalary >= 10.0)
totalHourlySalary = totalHourlySalary + hourlySalary;