如何使用NumericUpDown计算文本框值的总和? - c#

我正在尝试制作一个小的比萨饼订购单,但是计算有问题。选择比萨饼后,单价和总计计算即可,但是选择添加项会带来问题。更改NumericUpDown值后,卡路里不正确(所有单位的价格和卡路里均不变)。 NumericUpDown的名称是numberofunit。我该如何计算?

if (pepper.Checked)
{
    string peppereklendi = 
        Convert.ToString(Convert.ToDouble(unitprice.Text)+ pepperprice);

    unitprice.Text = peppereklendi;

    total.Text = 
        Convert.ToString(Convert.ToDecimal(unitprice.Text) * numberofunit.Value);

    string pepperkaloriekle = 
        Convert.ToString(Convert.ToInt16(gizlikalori.Text) + pepperkalori);

    gizlikalori.Text = pepperkaloriekle;

    amountofcalorie.Text = 
        Convert.ToString(Convert.ToDecimal(gizlikalori.Text) * numberofunit.Value);
}
else
{
    string peppereklendi = unitprice.Text;

    unitprice.Text = 
        Convert.ToString(Convert.ToDouble(peppereklendi) - pepperprice);

    total.Text = Convert.ToString(Convert.ToDecimal(unitprice.Text) * numberofunit.Value);

    string pepperkaloriekle = gizlikalori.Text;

    gizlikalori.Text = 
        Convert.ToString(Convert.ToDouble(pepperkaloriekle) - pepperkalori);

    amountofcalorie.Text = 
        Convert.ToString(Convert.ToDecimal(gizlikalori.Text) * numberofunit.Value);
}

此代码是Pepper的复选框代码。

This is the form of my application.

参考方案

您实际上应该尝试将计算逻辑与UI逻辑(窗体)分开。然后,事情将变得更加清晰:

// Get values from the text boxes
decimal up = Convert.ToDecimal(unitprice.Text);
decimal calories = Convert.ToDecimal(gizlikalori.Text);
decimal tot, totCalories;

// Do the calculation
if (pepper.Checked) {
    up = up + pepperprice;
    calories = calories + pepperkalori;
}
tot = up * numberofunit.Value;
totCalories = calories * numberofunit.Value;

// Assign the results to text boxes
unitprice.Text = up.ToString();
total.Text = tot.ToString();
gizlikalori.Text = calories.ToString();
amountofcalorie.Text = totCalories.ToString();

您做错了,是从单价中减去胡椒价格和胡椒卡路里,如果未选择胡椒,则减去单位卡路里。但是,单价(和卡路里)已经没有胡椒了!

我看不到何时执行此计算,但是,如果每次增加单位数量都执行此计算,那么每次都将添加胡椒​​价格!对于基础单价,最好有一个单独的变量,当您检查添加项时,该变量保持不变。然后,总是从基本单价开始计算。

另外,您正在混合许多不同的数字类型。这是没有道理的。

进一步增强代码的下一步将是为计算创建一个单独的类。您也可以使用数据绑定。这将完全消除进行转换的需要。查看我对以下帖子的回答
:manipulating-textbox-variables-in-calculations

不会将toString()方法中的super关键字隐式转换为super.toString() - java

我有以下两个类,如下所示。为了简单起见,仅显示toString重写的方法。 public class Circle { @Override public String toString() { return "Circle"; } } public class Cylinder extends Circle { @Override pub…

单击选项卡链接时,请专注于每个引导选项卡中的First asp:textbox - javascript

我是开发的新手,并开始开发简单的asp.net应用程序。我正在使用每个都有一些asp标签和文本框的bootstrap选项卡。单击该选项卡时,我要重点关注选项卡内容中的第一个文本框。我搜索了各种答案,但都是针对输入字段的(exp:输入type =“ text”)。找不到适用于ASP文本框的任何内容。任何帮助将不胜感激。谢谢 javascript参考方案 ASP…

将变量另存为txt但不在服务器中 - php

如何为用户保存一些变量到txt文件中?不想生成并存储在我的服务器中,只想生成然后用户保存在他的PC上,服务器上没有任何变化顺便说一句,此操作是否占用大量资源?谢谢 参考方案 您可以这样做:header('Content-Type: text/plain'); header('Content-Disposition: attach…

LeetCode题解计算机为什么是基于二进制的?

可以是三进制么?二进制有什么好处?题解:为什么叫电子计算机?算盘应该没有二进制

LeetCode题解统计城市的所有灯泡

这个是我刚毕业的时候,一个真实的面试题,这是一个开放题。题目描述:想办法,将一个城市的所有灯泡数量统计出来。题解:费米估算法1、如果某个城市常驻人口有1000万2、假设每5人居住在一套房里,每套房有灯泡5只,那么住宅灯泡共有1000万只3、假设公众场所每10人共享一只灯泡,那么共有100万只4、主要的这两者相加就得出了1100万只当然实际上这是估算的,具体应…