选中这两个复选框时,这是我想要的输入:
* *输入:小时= 45,评分10.00,检查医疗/牙科和401k复选框
这是我期望的输出:
·产出:毛薪= 475.00,医疗/牙科扣除= 50.00,401k扣除= 23.75,税= 100.31,净工资= 300.94 **
但是,当我按下计算按钮并选中两个复选框(忽略名称)后运行我的项目时,我收到了这个:
姓名:乔 营业时间:45 费率:10.00 总薪酬:400.00美元 税金:112.50美元 净工资:337.50美元 医疗/牙科扣除:400.00美元 401k扣除:$ 20.00
对于我做错了什么的任何帮助将不胜感激。我似乎无法弄清楚问题。
这就是我项目中的内容:
这些变量在代码顶部声明:
private const decimal TAX = 0.25m;
private string name = "";
private decimal Gross_pay;
private decimal Taxes;
private decimal Net_Pay;
private decimal annual_salary;
private int NumberOfEmployees;
private decimal deductionMed;
private decimal deduction401k ;
这是计算发生的地方:
private void CalcButton_Click(object sender, EventArgs e)
{ // The “Calculate” button calculates gross pay, taxes, and net pay and then displays name, department, gross pay, taxes, and net pay using currency format for various amounts in the rich text box
// Gross pay= (hours * rate)
// Taxes= (25% of gross pay)
// Net pay (gross pay ?taxes)
//calculate
Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text);
Taxes = TAX * Gross_pay;
Net_Pay = Gross_pay - Taxes;
annual_salary = Net_Pay;
Taxes = TAX * (Gross_pay - (deductionMed + deduction401k));
//overtime pay
if (Convert.ToInt32(HoursTextBox.Text) >= 41)
{
Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) * 1.5m;
DisplayOutPut.Text += "\nOvertime:" + Gross_pay.ToString("C") + "\n";
}
//Medical/Dental and 401k deductions...as well as tax collected.
if (MedicalDentalDeductions.Checked)
{
deductionMed = Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) - 50.00m;
}
if (FourOneKDeduction.Checked)
{
deduction401k = Gross_pay * 0.05m;
}
//display
DisplayOutPut.Text = "Name: "+ "";
DisplayOutPut.Text += NameTextBox.Text + "\n";
DisplayOutPut.Text += "Hours: " + HoursTextBox.Text + "\n";
DisplayOutPut.Text += "Rate: " + RateTextBox.Text + "\n";
DisplayOutPut.Text += "Gross Pay: " + Gross_pay.ToString("C") + "\n"; // Hours*Rate
DisplayOutPut.Text += "Taxes: " + Taxes.ToString("C") + "\n";
DisplayOutPut.Text += "Net Pay: " + Net_Pay.ToString("C");
DisplayOutPut.Text += "\nMedical/Dental deduction: " + deductionMed.ToString("C") + "\n401k deduction: " + deduction401k.ToString("C");
//handling the invalid inputs
if (NameTextBox.Text == "")
{ MessageBox.Show("Name is missing.", "Error"); }
if (Convert.ToInt32(HoursTextBox.Text) >= 70)
{ MessageBox.Show("Please Enter a Valid hour.", "Invalid data type."); }
if (RateTextBox.Text == "" && (RateTextBox.Text == ","))
{ MessageBox.Show("Please Enter a valid amount.", "Invalid data type ($)"); }
if (Convert.ToInt32(HoursTextBox.Text) >= 70)
{ MessageBox.Show("You have exceeded the maximum hours per week."); }
else if (Convert.ToInt32(HoursTextBox.Text) < 10)
{ MessageBox.Show("You cannot input less than 10 hours."); }
if (Convert.ToDecimal(RateTextBox.Text) < 9.75m)
{ MessageBox.Show("Please enter the minimum wage."); }
}
这些是复选框的方法:
private void MedicalDentalDeductions_CheckedChanged(object sender, EventArgs e)
{
}
private void FourOneKDeduction_CheckedChanged(object sender, EventArgs e)
{
}
答案 0 :(得分:1)
您应该将计算分成不同的方法,因为它更容易调试。在说,我没有测试以下,但每个方法应该产生你想要的值。只需使用从这些方法派生的值来构建DisplayOutPut(我想留下一些工作要做)。
你应该计算的第一件事是总薪水:
private double calculateGrossPay (double hours, double rate)
{
double result = 0.00;
double standardHours = 0.00;
double overtimeHours = 0.00;
if (hours > 40)
{
overtimeHours = (hours - 40) * (rate * 1.5);
standardHours = 40 * rate;
}
else
{
standardHours = hours * rate;
}
result = standardHours + overtimeHours;
return result;
}
然后计算税:
private double caculateTax (double gross, double tax)
{
double result = 0.00;
result = gross * tax; // assuming tax is represented as 0.25 for 25%
return result;
}
然后计算扣除额:
private double caclulate401k (double gross) // Or net?
{
double result = 0.00;
result = gross * 0.05;
return result;
}
private double calculateMedical (double gross) // Or net?
{
double result = 0.00;
result = 50.00; // I figure this should be an actual calculation, if medical is always $50, waste of a method...
return result;
}
最后,计算净值。
private double calculateNet (double gross, double tax, double med, double 401k)
{
double result = 0.00;
double deductions = tax + med + 401k;
result = gross - deductions;
return result;
}