我有两个正确的计算,零件和税,但服务和劳动力以及总数是错误的。对我所忽略的内容的任何见解都将不胜感激。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//method for calculating oil and lube charges
private int OilLubeCharges()
{
int total = 0;
//if oil is checked, add 28 to total
if (chkOilChange.Checked)
{
total += 26;
}
//if lube is checked, add 18 to total
if (chkLube.Checked)
{
total += 18;
return total;
}
//If nothing is checked return 0
else
{
return total;
}
}
//method for calculating Flushes
private int FlushCharges()
{
//define local variable
int total = 0;
//if radiator is checked, add 30 to total
if (chkRadiator.Checked)
{
total += 30;
//if transmission is checked, add 80 to total
if (chkTransmission.Checked)
{
total += 80;
return total;
}
//if nothing is checked return 0
else
{
return total;
}
}
//if nothing is checked return 0
else
{
return total;
}
}
private int MiscCharges()
{
//define local variable
int total = 0;
//if inspection is checked, add to total
if (chkInspection.Checked)
{
total += 15;
}
//if replace muffler is checked, add 100 to total
if (chkMuffler.Checked)
{
total += 100;
}
//if tire rotation is checked, add 20 to total
if (chkTire.Checked)
{
total += 20;
return total;
}
//if nothing is checked return 0
else
{
return total;
}
}
//calculate the total for the other charges
private int OtherCharges()
{
int total = 0;
int labor;
decimal parts;
if (int.TryParse(txtLabor.Text, out labor))
{
total = labor;
}
if (decimal.TryParse(txtParts.Text, out parts))
{
txtPartsTotal.Text = parts.ToString("c");
return total;
}
else
{
return total;
}
}
private decimal TaxCharges()
{
decimal addTax;
string parts = txtParts.Text;
addTax = Convert.ToDecimal(parts) * 0.06m;
txtTax.Text = addTax.ToString("c");
return addTax;
}
private decimal TotalCharges()
{
decimal total;
decimal serviceAndLabor;
total = OtherCharges() + MiscCharges() + OilLubeCharges() + FlushCharges() + TaxCharges();
txtTotal.Text = total.ToString("c");
serviceAndLabor = MiscCharges() + OilLubeCharges() + FlushCharges() + OtherCharges();
txtServiceAndLabor.Text = serviceAndLabor.ToString("c");
return total;
}
private void btnCalculate_Click(object sender, EventArgs e)
{
OilLubeCharges();
FlushCharges();
MiscCharges();
OtherCharges();
TaxCharges();
TotalCharges();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
答案 0 :(得分:0)
在所有这些"魔术数字"中间我会提请你注意这段代码
//if oil is checked, add 28 to total
if (chkOilChange.Checked)
{
total += 26;
}
您希望在总数中添加28,但只添加了26
答案 1 :(得分:0)
我发现了问题。在我的旧代码中,我有一个if语句嵌套到另一个语句中它应该没有。最重要的是,我在方法中间返回了一些内容,因此它从未达到过一个语句。
//if radiator is checked, add 30 to total
if (chkRadiator.Checked)
{
total += 30;
//if transmission is checked, add 80 to total
if (chkTransmission.Checked)
{
total += 80;
return total;
}
//if nothing is checked return 0
else
{
return total;
}
}
//if nothing is checked return 0
else
{
return total;
}