C# 程序计算不正确
我有两个计算是正确的,零件和税,但服务和劳动力以及总数是错误的。如果您能了解我所忽略的内容,我们将不胜感激 代码:C# 程序计算不正确,c#,C#,我有两个计算是正确的,零件和税,但服务和劳动力以及总数是错误的。如果您能了解我所忽略的内容,我们将不胜感激 代码: 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
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();
}
}
}
在所有这些“神奇数字”的中间,我会注意到这个代码片段
//if oil is checked, add 28 to total
if (chkOilChange.Checked)
{
total += 26;
}
你想在总数中加28,但只加了26。我发现了问题。在我的旧代码中,当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;
}
你的问题很不清楚。出问题的实际计算是什么?什么是输入值,什么是预期输出,什么是实际输出。你应该让人们尽可能容易地发现问题,而不必翻阅3页的源代码。你试过了吗?您的预期输出是什么,而实际输出是什么?抱歉,total calculations方法中的具体计算混乱了。由于某种原因被选中的复选框加起来不正确。此外,预期输出取决于用户选择的内容。我尝试过调试,但没有真正起到帮助。然后发布一个用户输入的具体示例,预期的输出是什么,正在输出什么。下面是程序运行的图片。所选项目的预期总产出应该是$286.60,但正如您所看到的,只有$146.60。服务费和人工费应该是276美元,但只有146美元。实际上应该是26美元,我的评论错了。