0

当两个复选框都被选中时,这是我想要的输入:

* *输入:小时 = 45,费率 10.00,医疗/牙科和 401k 复选框均已选中

这是我期望的输出:

· 输出:总工资 = 475.00,医疗/牙科扣除 = 50.00,401k 扣除 = 23.75,税收 = 100.31,净工资 = 300.94**

但是,当我在点击计算按钮并选中两个复选框(忽略名称)后运行我的项目时,我会收到此消息:

姓名:Joe 工作时间: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)
    {


      }
4

1 回答 1

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;  
}
于 2013-10-15T02:18:35.930 回答