简体   繁体   English

C#数学问题

[英]C# Math Problem

I have been working on this for the greater part of the day and I cant seem to make this part of my code work. 在一天的大部分时间里,我一直在从事此工作,而我似乎无法使我的代码工作于此。 The intent of the code is to allow the user to input a set of values in order to calculate the missing value. 该代码的目的是允许用户输入一组值以计算缺失值。 As an additional feature I placed a CheckBox on the form to allow the user to do further calculation. 作为一项附加功能,我在窗体上放置了一个CheckBox,以允许用户进行进一步的计算。 That is where my problem lies. 那就是我的问题所在。 I know the code works because if I change the formula the value that appears in tb3_aic.Text changes per the formula. 我知道代码有效,因为如果更改公式,则出现在tb3_aic.Text中的值会根据公式更改。 However, when I use the below the answer does not change like it should. 但是,当我使用以下内容时,答案不会像应有的那样改变。 Please reference the attached code. 请参考随附的代码。 If a jpg image is needed of the formula I can e-mail it. 如果需要该公式的jpg图片,我可以通过电子邮件发送。

 void Calc3Click(object sender, EventArgs e)

    {
       if (String.IsNullOrEmpty(tb3_skv.Text) | String.IsNullOrEmpty(tb3_kva.Text) | String.IsNullOrEmpty(tb3_z.Text))
    {
        MessageBox.Show("Enter all required values", "Missing Data", MessageBoxButtons.OK);
    }   //If user does not enter all the values required for the calculation show error message box
    else

    {
        if (!String.IsNullOrEmpty(tb3_skv.Text) & !String.IsNullOrEmpty(tb3_kva.Text) & !String.IsNullOrEmpty(tb3_z.Text))

        { //If motor load check box is not checked and required values are entered calculate AIC based on formula.
            int y; 
            decimal x, z, a;
            x = decimal.Parse(tb3_skv.Text);      
            y = int.Parse(tb3_kva.Text);
            a = decimal.Parse(tb3_z.Text);
            z = (y * 1000) / (x * 1.732050808m) / (a / 100); //the m at the end of the decimal allows for the multiplication of decimals
            tb3_aic.Text = z.ToString();
            tb3_aic.Text = Math.Round(z,0).ToString();
        }


        if (cb3_ml.Checked==true) 
        {//If Motor Load CB is checked calculate the following
            int y, b;
            decimal x, z, a;
            x = decimal.Parse(tb3_skv.Text);
            y = int.Parse(tb3_kva.Text);
            a = decimal.Parse(tb3_z.Text);
            b = int.Parse(tb3_ml.Text);
            z = ((y * 1000) / (x * 1.732050808m) / (a / 100))+((b / 100)*(6*y)/(x*1.732050808m)*1000);
            tb3_aic.Text = z.ToString();
            tb3_aic.Text = Math.Round(z,5).ToString();
        }

     }

I am grateful for any help that can be provided. 感谢您能提供的任何帮助。

Thank you, Greg Rutledge 谢谢Greg Rutledge

Without really knowing what the problem is, a few things look a bit odd: 在不真正知道问题出在哪里的情况下,有些事情看起来有些奇怪:

  • Also, mixing decimal and int in a calculation can lead to unexpected results unless you really know what you are doing. 此外,除非您真的知道自己在做什么,否则在计算中混合使用十进制和整数可能会导致意外结果。 I suggest using only decimals (or doubles, which are way faster and usually have enough precision for engineering computations). 我建议仅使用小数(或小数,这是更快的方式,通常对于工程计算而言具有足够的精度)。
  • You also set tb3_aic.Text twice, I assume you decided on rounding later and forgot to remove the first one. 您还设置了tb3_aic.Text两次,我假设您决定稍后舍入并忘记删除第一个。

Edit: This is apparently wrong in C#: 编辑:这在C#中显然是错误的:

  • You use bit-wise AND (&) in the if-clause where you probably mean logical AND (&&). 您可以在if子句中使用按位与(&),在这里您可能表示逻辑与(&&)。

The only difference between them when used with boolean operands are that && short circuits (doesn't evaluate the right operand if the left is false). 与布尔操作数一起使用时,它们之间的唯一区别是&&短路(如果left为false,则不评估右侧的操作数)。 I learned something, thank you people commenting. 我学到了一些东西,谢谢大家的评论。 :) :)

Change this: 更改此:

int y, b;

To this: 对此:

int y;
decimal b;

and it works, according to this test: 根据此测试,它可以正常工作:

    public void Test2()
    {
        int y;
        decimal b;
        decimal x, z, a;
        decimal z1, z2;
        x = 480m;
        y = 2500;
        a = 5.75m;
        b = 10;
        z1 = ((y * 1000) / (x * 1.732050808m) / (a / 100));
        z2 = ((b / 100) * (6 * y) / (x * 1.732050808m) * 1000);
        z = z1 + z2;
        Console.WriteLine("{0}, {1}", z1, z2);
        Console.WriteLine(Math.Round(z, 0).ToString());
    }

The reason is that integer 10 divided by integer 100 is integer 0, which zero's the whole z2. 原因是整数10除以整数100就是整数0,这是整个z2的零。

I don't know if this is huge but you are setting the text of the tb3_aic textbox twice when maybe you are trying to concatenate? 我不知道它是否很大,但是当您尝试连接时,您要两次设置tb3_aic文本框的文本?

tb3_aic.Text = z.ToString();
tb3_aic.Text = Math.Round(z,0).ToString();

You seem to be doing this in both methods. 您似乎在两种方法中都这样做。

The more that I look at it, this could be an issue since you are setting it in the first if then the second If will overwrite that if the checkbox is checked. 我查看的次数越多,这可能是一个问题,因为如果您在第一个If中设置了该属性,则在第二个If中设置了该复选框(如果选中该复选框)。

Sample Inputs, first formula x = 480 y = 2500 a = 5.75 样本输入,第一个公式x = 480 y = 2500 a = 5.75

For the first formula the output should be 52296 对于第一个公式,输出应为52296

Sample Inputs, second formula x = 480 y = 2500 a = 5.75 b = 10 样本输入,第二个公式x = 480 y = 2500 a = 5.75 b = 10

For the first formula the output should be 54100.42 对于第一个公式,输出应为54100.42

Hope this helps 希望这可以帮助

不需要此行,尽管它不是错误,但总会评估为true

if (!String.IsNullOrEmpty(tb3_skv.Text) & !String.IsNullOrEmpty(tb3_kva.Text) & !String.IsNullOrEmpty(tb3_z.Text)) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM