循环条件
Loop conditions
我仍在学习 C#,我有一个关于循环条件的快速问题。我会尽可能清楚地解释我的问题,所以如果我混淆了任何人,我会提前道歉。所以假设我有一个数字,它是从用户输入的双精度形式的(我选择了一个双精度,因为我主要使用它们,但这不是全部,这意味着它可以是小数,也可以是整数).现在在我的循环中,我想设置条件以确保数字不是字符(即用户输入字母而不是数字。),这样当发生这种情况时,将出现一个消息框,说只能输入数字。我之所以问这个,是因为我不知道如何编写代码,而且每次我 运行 程序时,整个程序都会停止,因为输入的格式不正确。我如何编写我的循环以验证输入实际上是数字而不是字母?我想要一个消息框弹出窗口,上面写着 "Enter numbers only please"。除了编写这部分代码之外,我知道如何做其他所有事情。我试过天知道在互联网上以这些形式问过这个问题多少次,但我从来没有得到明确的答案。
对于那些需要查看某种代码以更好地理解它的人,如下所示(仅供参考,我使用的是 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:https://msdn.microsoft.com/en-us/library/26sxas5t(v=vs.110).aspx
TryParse returns 一个布尔值,告诉您转换是否失败,如果成功,输出值将是您要查找的结果。希望这对您有所帮助!
您确实应该研究一下如何在 .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;
}
我仍在学习 C#,我有一个关于循环条件的快速问题。我会尽可能清楚地解释我的问题,所以如果我混淆了任何人,我会提前道歉。所以假设我有一个数字,它是从用户输入的双精度形式的(我选择了一个双精度,因为我主要使用它们,但这不是全部,这意味着它可以是小数,也可以是整数).现在在我的循环中,我想设置条件以确保数字不是字符(即用户输入字母而不是数字。),这样当发生这种情况时,将出现一个消息框,说只能输入数字。我之所以问这个,是因为我不知道如何编写代码,而且每次我 运行 程序时,整个程序都会停止,因为输入的格式不正确。我如何编写我的循环以验证输入实际上是数字而不是字母?我想要一个消息框弹出窗口,上面写着 "Enter numbers only please"。除了编写这部分代码之外,我知道如何做其他所有事情。我试过天知道在互联网上以这些形式问过这个问题多少次,但我从来没有得到明确的答案。 对于那些需要查看某种代码以更好地理解它的人,如下所示(仅供参考,我使用的是 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:https://msdn.microsoft.com/en-us/library/26sxas5t(v=vs.110).aspx
TryParse returns 一个布尔值,告诉您转换是否失败,如果成功,输出值将是您要查找的结果。希望这对您有所帮助!
您确实应该研究一下如何在 .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;
}