简体   繁体   English

按钮不会使值显示在textBox中

[英]Button doesn't make value appear in textBox

I want 2 textBoxes to show numbers after I press a button, but when I press a button, one textBox shows number in it and the second doesn't. 我想2个文本框后我按下一个按钮显示的数字,但是当我按下一个按钮,了一个TextBox显示其数量和第二个没有。

    private void Calculating(object sender, EventArgs e)
    {
        if (textBox6.Text != "")
        {
           rutr = Double.Parse(textBox6.Text);
           rutd = rutr * 2;            
           textBox7.Text = (rutd).ToString();
           textBox8.Text = (3 / 4 * pi * rutr * rutr * rutr).ToString();
        }
    }

The textBox8 doesn't show correct number. textBox8没有显示正确的数字。

The very cause of the misbehavior is integer division : 3 / 4 == 0 should be 行为不当的根本原因是整数除法3 / 4 == 0应该是

// The formula seems to be a volume of a sphere: 3/4 * Pi * r**3 
// It's a physical world, that's why 3.0 - double, not 3M - decimal 
textBox8.Text = (3.0 / 4.0 * pi * rutr * rutr * rutr).ToString();

please, notice floating point 3.0 value instead of integer 3 . 请注意浮点数 3.0而不是整数 3 Another suggestion is to use double.TryParse : if we can parse user input ( textBox6.Text ) then do it and compute 另一个建议是使用double.TryParse :如果我们可以解析用户输入( textBox6.Text ),然后进行计算

private void Calculating(object sender, EventArgs e) {
  double rutr = 0.0;

  // If we can parse textBox6.Text into double rutr  
  if (double.TryParse(textBox6.Text, out rutr)) {
    rutd = rutr * 2;            
    textBox7.Text = (rutd).ToString();
    textBox8.Text = (3.0 / 4.0 * pi * rutr * rutr * rutr).ToString(); 
  }
}

Edit : Technically, it's possible to produce blank ( "" ) in the textBox8 , see comments below (it's an interesting problem itself). 编辑 :从技术上讲,有可能textBox8产生空白"" ),请参阅下面的评论(这本身就是一个有趣的问题)。 Here's the code 这是代码

    using System.Globalization;

    ...

    CultureInfo ci = (CultureInfo.CurrentCulture.Clone() as CultureInfo);

    // The idea: double.NaN will be displayed as blank 
    ci.NumberFormat.NaNSymbol = "";   

    CultureInfo.CurrentCulture = ci;  

    double pi = Math.Sqrt(-1); // pi is double.NaN - imaginary unit 

    ...

    // 0 * NaN * rutr * rutr * rutr == NaN which is printed as empty string
    textBox8.Text = (3 / 4 * pi * rutr * rutr * rutr).ToString(); 

Here we exploit the fact that, 0 * double.NaN == double.NaN . 在这里,我们利用以下事实: 0 * double.NaN == double.NaN However, I don't believe in this kind of error 但是,我不相信这种错误

You should use a suffix to tell the compiler that the numeric literals are not treated as int. 您应该使用后缀来告诉编译器数字文字不被视为int。

textBox8.Text = (3M / 4M * pi * rutr * rutr * rutr).ToString();

https://docs.microsoft.com/en-US/dotnet/csharp/language-reference/keywords/decimal https://docs.microsoft.com/en-US/dotnet/csharp/language-reference/keywords/decimal

Here is a list of possible suffixes: 以下是可能的后缀列表:

F: float D: double U: uint L: long UL: ulong M: decimal F:浮点数D:双精度U:uint L:长UL:ulong M:十进制

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

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