简体   繁体   English

C#bool语句为看似未连接的double.parse(string)引发了奇怪的异常

[英]C# bool statement throws strange exception for seemingly unconnected double.parse(string)

I'm an absolute beginner when it comes to C#. 对于C#,我是绝对的初学者。 Trying to learn via examples. 尝试通过示例学习。 So I've found myself a nice little calculator tutorial. 所以我发现自己是一个不错的计算器教程。 Everything goes fine up to last moment, the code is working, but it doesn't take multi-digit input like 33 . 一切都进行到最后一刻,代码可以正常工作,但是它不需要像33那样的多位输入。 There's a bool statement there for turning arithmetic operations on/off and tutorial instructor figured, that we should put bool = false before the number input/button press (in button_Click ). 那里有一个bool语句,用于打开/关闭算术运算,并且辅导老师认为,我们应该在数字输入/按钮按下之前(在button_Click )将bool = false button_Click

His code looks like this: 他的代码如下所示:

public partial class MainWindow : Window
{
    double value = 0;
    string operation = "";
    bool operation_pressed = false;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        if ((tb.Text == "0") || (operation_pressed == true))
           tb.Clear();

        operation_pressed = false;
        Button b = (Button)sender;
        tb.Text += "\n" + b.Content.ToString();
    }

    private void operator_Click(object sender, RoutedEventArgs e)
    {
        Button b = (Button)sender;
        operation = b.Content.ToString();
        value = double.Parse(tb.Text);
        operation_pressed = true;
        equation.Content = value + " " + operation;
    }

    private void result_Click(object sender, RoutedEventArgs e)
    {

        equation.Content = "";
        switch(operation)
        {
            case "+":
                tb.Text = "\n" + (value + double.Parse(tb.Text)).ToString();
                break;
            case "-":
                tb.Text = "\n" + (value - double.Parse(tb.Text)).ToString();
                break;
            case "*":
                tb.Text = "\n" + (value * double.Parse(tb.Text)).ToString();
                break;
            case "/":
                tb.Text = "\n" + (value / double.Parse(tb.Text)).ToString();
                break;
            default:
                break;
        }
    }

    private void CE_Click(object sender, RoutedEventArgs e)
    {
        tb.Text = "\n 0";
    }

    private void C_Click(object sender, RoutedEventArgs e)
    {
        tb.Clear();
        equation.Content = "";
        value = 0;
    }
}

It compiles nicely. 它可以很好地编译。 But when I try to input a multidigit number and follow it with a mathematical operator, it throws an exception for value = double.Parse(tb.Text); 但是,当我尝试输入多位数并用数学运算符跟随它时,它会抛出异常, value = double.Parse(tb.Text); that states: 声明:

When converting string to DateTime, parse the string to take the date before putting each variable into the DateTime object. 将字符串转换为DateTime时,在将每个变量放入DateTime对象之前,分析字符串以获取日期。

I'm so confused right now. 我现在很困惑。 There's no DateTime even involved! 甚至没有涉及DateTime And I'm 100% positive, everything is like in the tutorial. 我是100%积极的,一切都像教程中一样。 What's going on? 这是怎么回事? :/ :/

Any help will be appreciated greatly! 任何帮助将不胜感激!

EDIT 编辑

Screenshot of actual error: 实际错误的屏幕截图:

错误

First of all, you're interpreting the debugger incorrectly. 首先,您错误地解释了调试器。 This is not the error message: 不是错误消息:

When converting string to DateTime, parse the string to take the date before putting each variable into the DateTime object. 将字符串转换为DateTime时,在将每个变量放入DateTime对象之前,分析字符串以获取日期。

Notice how it's listed as "Troubleshooting Tips". 请注意,它是如何作为“故障排除技巧”列出的。 In the vast majority of cases, you can ignore it. 在大多数情况下,您可以忽略它。 The error message itself is in a language I don't know, so I can't speak to what it says. 错误消息本身使用的语言我不知道,所以我无法说出它的意思。 But a FormatException essentially means that you're trying to parse a value which can't be parsed. 但是FormatException本质上意味着您试图解析一个无法解析的值。

Your screen shot cuts off some information, but what is the value of tb.Text ? 屏幕截图会切断一些信息,但是tb.Text的值是tb.Text Is it one of those "+" strings? 是那些"+"字符串之一吗? If so, then that's your problem. 如果是这样,那就是你的问题。

"+" can't be parsed as a numeric value, because "+" isn't a number. 无法将"+"解析为数值,因为“ +”不是数字。

You can make your code a little more resistant to errors by using TryParse instead of Parse . 通过使用TryParse而不是Parse可以使代码对错误的抵抗能力更高。 Something like this: 像这样:

double result;
if (!double.TryParse(tb.Text, out result))
{
    // couldn't parse
}

If the if block isn't entered, then result will contain the successfully parsed value. 如果未输入if块,则result将包含成功解析的值。 If it is entered, then the value couldn't be parsed. 如果输入它,则该值无法解析。 How you handle that situation is up to you. 如何处理这种情况取决于您。 An error message to the user, a default value instead of the parsed value, etc. That's application logic for you to define. 给用户的错误消息,默认值而不是已解析的值等。这是您定义的应用程序逻辑。

The point is, tb.Text contains a non-numeric value which you're trying to convert into a numeric value. 关键是tb.Text包含一个非数字值,您试图将其转换为数字值。 Hence the error. 因此,错误。

Try this code in operator_Click event 在operator_Click事件中尝试此代码

Button b = (Button)sender;
operation = b.Text;
value = Convert.ToDouble(tb.text)

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

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