C# Can';t将基价输出到小计
我正在尝试创建一个计算器,它可以将基本价格、添加的任何单选按钮/复选标记加上税金,并将结果放入小计。为什么我不能让我的基本价格文本框出现在小计文本框中?我试过几种方法,但似乎不管用。有什么建议吗? 这是我的密码:C# Can';t将基价输出到小计,c#,winforms,calculator,C#,Winforms,Calculator,我正在尝试创建一个计算器,它可以将基本价格、添加的任何单选按钮/复选标记加上税金,并将结果放入小计。为什么我不能让我的基本价格文本框出现在小计文本框中?我试过几种方法,但似乎不管用。有什么建议吗? 这是我的密码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void txttradein_Text
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void txttradein_TextChanged(object sender, EventArgs e)
{
lblTradein.Text = txtTradeIn.Text;
}
private void btnCalculate_Click(object sender, EventArgs e)
{
double BasePrice = double.Parse(txtBasePrice.Text);
try
{
if (BasePrice < 0)
{
MessageBox.Show("Error");
txtBasePrice.Focus();
txtBasePrice.SelectAll();
}
}
catch (FormatException)
{
txtBasePrice.SelectAll();
MessageBox.Show("Numbers Only");
}
try
{
double TradeIn= double.Parse(txtTradeIn.Text);
if(TradeIn < 0)
{
MessageBox.Show("Error");
txtTradeIn.Focus();
txtTradeIn.SelectAll();
}
}
catch (FormatException)
{
txtTradeIn.SelectAll();
MessageBox.Show("Numbers Only");
}
double stereoPrice = 0, leatherPrice = 0, navPrice = 0;
if (chkStereo.Checked) {
stereoPrice = 425.76;
} if (chkLeather.Checked){
leatherPrice = 987.41;
} if (chkComputer.Checked){
navPrice = 1741.23;
}
double exteriorFinish = 0;
if(radPearlized.Checked)
{
exteriorFinish = 345.72;
}
else if (radCustom.Checked)
{
exteriorFinish = 599.99;
}
else
{
exteriorFinish = 0;
}
double subTotal = stereoPrice + leatherPrice + navPrice + exteriorFinish;
double txtRate = 0.08, tax = 0;
tax = subTotal * txtRate;
txtTax.Text = tax.ToString();
txtSubtotal.Text = subTotal.ToString();
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
私有void txttradein_TextChanged(对象发送方,事件参数e)
{
lblTradein.Text=txtradein.Text;
}
私有void btnCalculate\u单击(对象发送者,事件参数e)
{
double BasePrice=double.Parse(txtBasePrice.Text);
尝试
{
如果(基准价格<0)
{
MessageBox.Show(“错误”);
txtBasePrice.Focus();
txtBasePrice.SelectAll();
}
}
捕获(格式化异常)
{
txtBasePrice.SelectAll();
MessageBox.Show(“仅限数字”);
}
尝试
{
double-TradeIn=double.Parse(txtradein.Text);
如果(贸易额<0)
{
MessageBox.Show(“错误”);
txtradein.Focus();
txtradein.SelectAll();
}
}
捕获(格式化异常)
{
txtradein.SelectAll();
MessageBox.Show(“仅限数字”);
}
双立体价格=0,皮革价格=0,导航价格=0;
如果(chkStereo.Checked){
立体价格=425.76;
}如果(chkLeather.Checked){
皮革价格=987.41;
}如果(chkComputer.Checked){
navPrice=1741.23;
}
双外表面光洁度=0;
如果(放射性珍珠化。检查)
{
表面光洁度=345.72;
}
否则如果(radCustom.Checked)
{
表面光洁度=599.99;
}
其他的
{
外观光洁度=0;
}
双倍小计=立体价格+皮革价格+导航价格+外观饰面;
双txtRate=0.08,tax=0;
税=小计*税率;
txtTax.Text=tax.ToString();
txtSubtotal.Text=subTotal.ToString();
}
}
}请在try块外声明基价变量。此变量在try块内声明,因此无法在外部访问 此外,在计算中甚至没有使用基价变量。见下文-
double subTotal = stereoPrice + leatherPrice + navPrice + exteriorFinish;
“…它们似乎不起作用…”-这些信息并没有真正的帮助。它没有说明什么不起作用,也就是说,您是否在任何地方收到任何错误消息,您是否尝试调试该问题以确保它确实执行了预期的操作。是的,我已经调试了它,到目前为止没有错误。但为了澄清我的基本价格并没有输出到我的小计中。这可能是什么原因造成的?在
尝试{}
块之外声明BasePrice
,并将BasePrice
添加到小计
@void我相信您会通过调试发现这一点。你在代码中检查变量了吗?是的,它仍然没有输出到SubTotalIt只是默认为零,它没有从baseprice输出我的输入。你的公式中没有使用baseprice。