我正在尝试编制Form Pay Estimator,它会计算单个员工的总工资,所欠税和净工资。出于某种原因,我的Pay()类中的CalculateTaxes方法没有返回正确的值。这是代码我在这里:我的代码没有返回正确的值
在我的工资()类:
{
//declare variables
private static int dependants;
private static double grossPay;
//property that gets and sets the dependants
public int NumOfDependents
{
get
{
return dependants;
}
set
{
dependants = value;
}
}
//calculates the gross pay for the production worker employee, using their hours
//worked times their wage and overtime is calculated for the employee, if
//applicable
public double CalculateGrossPay(double hoursWorked, double wageRate)
{
grossPay = hoursWorked * wageRate;
if (hoursWorked > 40)
{
grossPay += (.5 * wageRate) * (hoursWorked - 40);
}
return grossPay;
}
//calculates the gross pay for a salesperson, using their hours worked times their
//wage rate and then commission is calculated for the employee, based on their
//sales amount
public double CalculateGrossPay(double hoursWorked, double wageRate, double salesAmount)
{
grossPay = hoursWorked * wageRate;
if (salesAmount <= 10000)
{
grossPay += .02 * salesAmount;
}
else if (salesAmount > 10000)
{
grossPay += .04 * salesAmount;
}
return grossPay;
}
//calculates the taxes the employee has to pay
public static double CalculateTaxes()
{
int payCutoff = 100 + (100 * dependants);
if (grossPay <= payCutoff)
{
double taxesPaid = .1 * grossPay;
return taxesPaid;
}
else
{
double taxesPaid = (.1 * payCutoff) + (.2 * (grossPay - payCutoff));
return taxesPaid;
}
}
}
在我的窗体类:
{
public FormPayEstimator()
{
InitializeComponent();
}
//closes the application
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
//closes the application
private void buttonExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
//clears all text boxes, unchecks the salesperson check box, makes the sales amount
//text box and label invisible and sets the focus back to the hours worked text box
private void buttonClearForm_Click(object sender, EventArgs e)
{
textBoxDependants.Text = "";
textBoxGrossPay.Text = "";
textBoxHourlyWageRate.Text = "";
textBoxHoursWorked.Text = "";
textBoxNetPay.Text = "";
textBoxTaxes.Text = "";
checkBoxSalesperson.Checked = false;
textBoxSalesAmount.Visible = false;
labelSalesAmount.Visible = false;
textBoxHoursWorked.Focus();
}
//displays information about the program
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Pay Estimator - Version 1.0", "Pay Estimator", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//if the user checks the salesperson check box the sales amount text box and label
//become visible to the user and it sets the focus to the sales amount text box
private void checkBoxSalesperson_CheckedChanged(object sender, EventArgs e)
{
textBoxSalesAmount.Visible = true;
labelSalesAmount.Visible = true;
textBoxSalesAmount.Focus();
}
//displays the font dialog box and allows user to change their font
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.Font = textBoxHoursWorked.Font;
fontDialog1.Font = textBoxHourlyWageRate.Font;
fontDialog1.Font = textBoxDependants.Font;
fontDialog1.Font = textBoxGrossPay.Font;
fontDialog1.Font = textBoxTaxes.Font;
fontDialog1.Font = textBoxNetPay.Font;
fontDialog1.Font = textBoxSalesAmount.Font;
if (fontDialog1.ShowDialog() != DialogResult.Cancel)
{
textBoxHoursWorked.Font = fontDialog1.Font;
textBoxHourlyWageRate.Font = fontDialog1.Font;
textBoxDependants.Font = fontDialog1.Font;
textBoxGrossPay.Font = fontDialog1.Font;
textBoxTaxes.Font = fontDialog1.Font;
textBoxNetPay.Font = fontDialog1.Font;
textBoxSalesAmount.Font = fontDialog1.Font;
}
}
//displays the color dialog box and allows user to change thei font color
private void colorToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.Color = textBoxHoursWorked.ForeColor;
colorDialog1.Color = textBoxHourlyWageRate.ForeColor;
colorDialog1.Color = textBoxDependants.ForeColor;
colorDialog1.Color = textBoxGrossPay.ForeColor;
colorDialog1.Color = textBoxTaxes.ForeColor;
colorDialog1.Color = textBoxNetPay.ForeColor;
colorDialog1.Color = textBoxSalesAmount.ForeColor;
if (colorDialog1.ShowDialog() != DialogResult.Cancel)
{
textBoxHoursWorked.ForeColor = fontDialog1.Color;
textBoxHourlyWageRate.ForeColor = fontDialog1.Color;
textBoxDependants.ForeColor = fontDialog1.Color;
textBoxGrossPay.ForeColor = fontDialog1.Color;
textBoxTaxes.ForeColor = fontDialog1.Color;
textBoxNetPay.ForeColor = fontDialog1.Color;
textBoxSalesAmount.ForeColor = fontDialog1.Color;
}
}
//calculates the users total gross pay, their taxes owed and their net pay
private void buttonCompute_Click(object sender, EventArgs e)
{
//declares variables
string inValue;
double hours, rate, dependants, salesAmount, grossPay, taxes, netPay;
//assigns variables to values user entered in the hours worked, hourly wage
//rate and dependants text boxes
inValue = textBoxHoursWorked.Text;
hours = double.Parse(inValue);
inValue = textBoxHourlyWageRate.Text;
rate = double.Parse(inValue);
inValue = textBoxDependants.Text;
dependants = int.Parse(inValue);
//creates an instance of the Pay class and runs the CalculateGrossPay method
//for the production workers
Pay p1 = new Pay();
grossPay = p1.CalculateGrossPay(hours, rate);
//checks to see if the sales amount checkbox is checked and if it is, a value
//is assigned to the salesAmount text box, an instance of the pay class is
// createdand the CalculateGrossPay method for a salesperson is run
if (checkBoxSalesperson.Checked == true)
{
inValue = textBoxSalesAmount.Text;
salesAmount = double.Parse(inValue);
Pay p2 = new Pay();
grossPay = p2.CalculateGrossPay(hours, rate, salesAmount);
}
//displays the answer in the Gross Pay text box
textBoxGrossPay.Text = String.Format("{0:c}", grossPay).ToString();
//runs the CalculateTaxes method from the Pay class and displays the result in
//the taxes text box
taxes = Pay.CalculateTaxes();
textBoxTaxes.Text = String.Format("{0:c}", taxes).ToString();
//calculates the net pay for an employee and displays the result in the net pay
//text box
netPay = grossPay - taxes;
textBoxNetPay.Text = String.Format("{0:c}", netPay).ToString();
}
}
}
当我计算值,我当它仅仅是40美元时,得到了70美元的税收。谁能告诉我为什么会发生这种情况?
呃,真的很长的代码。你是否通过调试器运行它以找出问题的根源? – 2011-04-15 03:47:06