【问题标题】:Loop conditions循环条件
【发布时间】:2015-05-25 20:09:00
【问题描述】:

我还在学习 C#,我有一个关于循环条件的快速问题。我会尽量清楚地解释我的问题,所以如果我混淆了任何人,我会提前道歉。因此,假设我有一个从用户输入的双精度数字(我选择了一个双精度数字,因为我主要使用它们,但这并不包括所有内容,这意味着它可以是小数,也可以是整数)。现在在我的循环中,我希望设置条件以确保数字不是字符(即用户输入字母而不是数字。),这样当发生这种情况时,会出现一个消息框,说只输入数字。我只问这个是因为我不知道如何编写代码,每次我运行程序时,整个程序都会停止,因为输入的格式不正确。我如何编码我的循环以验证输入实际上是数字而不是字母?我想弹出一个消息框,上面写着“请仅输入数字”。除了编码这部分之外,我知道如何做其他所有事情。我试过天知道多少次在互联网上以这些形式提出这个问题,但我从来没有得到明确的答案。 对于那些需要查看某种代码以更好地理解的人,如下所示(仅供参考,我使用的是 Windows 窗体):

        double amnt = Convert.ToDouble(txtAMNT.Text);
        string Amount=txtAMNT.Text;
        double rate = Convert.ToDouble(txtRATE.Text);
        string Rate = txtRATE.Text;
        double time = Convert.ToDouble(txtTIME.Text);
        string Time=txtTIME.Text;
        double monthpay;
        double totalinterest;
        double realrate = (rate / 12)/100;

        if ((Amount == "")||(Rate == "")||(Time==""))
        {
            MessageBox.Show("Please fill all boxes with numbers");
        }
        else if (!(Rate == "."))
        {
            MessageBox.Show("Please enter the rate with a decimal sign ' . ' .");
        }
        else if (Amount == ",")
        {
            MessageBox.Show("Please enter the Amount without Commas ' , '");
        }
        else if ((Time == ",") || (Time == "."))
        {
            MessageBox.Show("Please enter the Duration in Months with out any decimals or commas.");
        }
        else
        {
            monthpay = amnt * realrate / (1 - Math.Pow(1 + realrate, -time));
            totalinterest = time * monthpay - amnt;
            mtbMonPay.Text = monthpay.ToString("c");
            mtbTotalInterest.Text = totalinterest.ToString("c");


        }

【问题讨论】:

  • 是什么让您认为您的问题与循环有关?您的问题不是关于循环,而是关于接收和解析用户输入。您的条件恰好是循环条件并不重要;您的直接问题是让条件(以及导致它的代码)正常工作。 (顺便说一句。如果您没有错误地声明问题的主题,您可能会得到更多/更好的答案。只是一个建议。:-)
  • 你应该研究TryParse方法
  • 不要使用TextBox
  • @Sayse - 感谢帮助的人,我不得不重新编码很多,但它有效! ^_^

标签: c# winforms loops conditional-statements


【解决方案1】:

最好的办法是使用 TryParse:https://msdn.microsoft.com/en-us/library/26sxas5t(v=vs.110).aspx

TryParse 返回一个布尔值,告诉您转换是否失败,如果转换成功,则输出值将具有您要查找的结果。希望这会有所帮助!

【讨论】:

    【解决方案2】:

    您确实应该对如何在 .Net 中解析用户输入进行一些研究。不过,您可以做以下简单的事情:

    static bool ParseField(string fieldName, string fieldValueText, out double fieldValue)
    {
        if (string.IsNullOrEmpty(fieldValueText)) 
        {
            MessageBox.Show(string.Format("Please provide an input value for '{0}'.", fieldName));
            return false;
        }
        else if (!double.TryParse(fieldValueText, out fieldValue))  
        {
            MessageBox.Show(string.Format("'{0}' is not a valid floating point value. Please provide a valid floating point input value for '{1}'.", fieldValueText, fieldName));
            return false;
        }
        return true;
    }
    

    这可以用于:

    bool GetInputs(out double amnt, out double rate, out double time)
    {
        if (ParseField("Amount", txtAMNT.Text, out amnt) &&
            ParseField("Rate", txtRATE.Text, out rate) &&
            ParseField("Time", txtTIME.Text, out time))
        {
            // Perform additional checks on individual values if needed.
            return true;
        }
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-24
      • 2015-08-19
      • 2015-05-23
      • 2020-11-12
      • 2020-01-23
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多