我正在制定一个程序,根据工时和工资率计算员工的工资。但是,当我尝试使用我在另一个类中创建的方法时:
public void GrossPay(int i)
{
double GrossPay = 0.0;
GrossPay = Hours[i] * PayRate[i];
Wages[i] = GrossPay;
}
尝试在我的主程序中使用它:
public static void Payroll()
{
int employeeID = 0;
int hours = 0;
double payRate = 0.0;
double wages = 0.0;
Payroll payroll = new Payroll(employeeID, hours, payRate, wages);
for (int i = 0; i < payroll.EmployeeID.Length; i++)
{
Console.WriteLine("Employee ID: {0}", payroll.EmployeeID[i]);
Console.WriteLine("How many hours does employee {0} work?", payroll.EmployeeID[i]);
string h = Console.ReadLine();
payroll.Hours[i] = Convert.ToInt32(h);
Console.WriteLine("What is the hourly wage for employee {0}?");
string w = Console.ReadLine();
payroll.PayRate[i] = Convert.ToInt32(w);
Console.WriteLine("Employee ID: {0} \nGross Pay: {1}", payroll.EmployeeID[i], payroll.Wages[i]);
Console.ReadLine();
}
}
无论如何,总薪酬都会返回0。请帮忙!
答案 0 :(得分:1)
你从未调用过你建立的方法;您应该在阅读Wages
属性之前调用它。
// ...
payroll.PayRate[i] = Convert.ToInt32(w);
payroll.GrossPay(i);
Console.WriteLine("Employee ID: {0} \nGross Pay: {1}", payroll.EmployeeID[i], payroll.Wages[i]);
// ...
这可以解决您的问题,但我建议您尽快给出object oriented paradigm。我建议您创建一个Employee
类,其中包含Id
,PayRate
,Hours
和属性Wages
等属性,如下所示:
public double Wages
{
get
{
return PayRate * Hours;
}
}
然后,Payroll
班级可能会有一系列员工:List<Employee> Employees;