在do ... while中将相关表达式匹配设置为false [英] Setting a relevant expression match to false in a do...while
本文介绍了在do ... while中将相关表达式匹配设置为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一些非常基本的代码,但是我也在正则表达式方面挑战自己.我一直可以弄清整个代码,但是我真正遇到的一个问题是,我试图在表达式为假的情况下运行do ... while循环.在这一点上,我绝对没有错误,但是do ... while循环一直在运行.
我在下面附上相关代码,希望对您有所帮助.
提前谢谢
if(tollResponse ==是"){Console.WriteLine(您每次旅行要付多少钱?");字符串tollTax = Console.ReadLine();匹配费用= Regex.Match(tollTax,@"[\ d.] +");如果(收费成功){Math.Round(Convert.ToDecimal(tollTax),2);Console.WriteLine(好主啊,这是你的钱");}别的{做{Console.WriteLine(请输入正确的数字");tollTax = Console.ReadLine();}while(toll.Success == false);}}
解决方案
简单的编码错误...在代码中添加了注释以解释问题
if(tollResponse ==是"){Console.WriteLine(您每次旅行要付多少钱?");字符串tollTax = Console.ReadLine();//toll.Success在这里设置.匹配费用= Regex.Match(tollTax,@"[\ d.] +");如果(收费成功){//不确定为什么要这样做,因为在给定的代码中没有使用它Math.Round(Convert.ToDecimal(tollTax),2);Console.WriteLine(好主啊,这是你的钱");}别的{//这是一个无限循环,因为toll.Success不再设置.做{Console.WriteLine(请输入正确的数字");tollTax = Console.ReadLine();} while(toll.Success == false);}}
我想你想要的
if(tollResponse ==是"){Console.WriteLine(您每次旅行要付多少钱?");//使用else语句遍历Console.ReadLine()并在第一次正确时退出做{字符串tollTax = Console.ReadLine();//toll.Success在这里设置.匹配费用= Regex.Match(tollTax,@"^ [+-]?[0-9] {1,3}(?:,?[0-9] {3})*(?:\.[0-9] {2})?$);如果(收费成功){Console.WriteLine(好主啊,这是你的钱");//将退出}别的{Console.WriteLine(请输入正确的数字");}} while(toll.Success == false);}
注意::还删除了1行重复代码,更新为使用我推荐的正则表达式,并删除了 Math.Round
正则表达式工具
[\ d.] +
说明:
数字(必需十进制)^ [+-]?[0-9] {1,3}(?:,?[0-9] {3})* \.[0-9] {2} $选项:不区分大小写在字符串«^»的开头声明位置匹配«[+-]?»下面列表中的单个字符在零到一遍之间,尽可能多地遍历,并根据需要回馈(贪婪)«?»字符"+"«+»人物 "-" "-"匹配"0"和"9"之间的单个字符«[0-9] {1,3}»在1至3次之间,尽可能多地进行,根据需要进行回馈(贪婪)«{1,3}»匹配«(?:,?[0-9] {3})*»下的正则表达式在0到无限制的时间之间,尽可能地多次,并根据需要进行回馈(贪婪)«*»匹配字符,",字面上是«,?»在零到一遍之间,尽可能多地遍历,并根据需要回馈(贪婪)«?»匹配"0"和"9"之间的单个字符«[0-9] {3}»恰好是"{3}"的3倍匹配字符."字面上地 "\."匹配"0"和"9"之间的单个字符«[0-9] {2}»恰好是"{2}"的2倍在字符串结尾处声明位置(或在字符串结尾处的换行符之前,如果有的话)«$»
将匹配:
1,432.01456.56654,246.43432321,5431423972921379312.32
将不匹配
324,123.432,,, 312,.32123,.23
取自我的答案
解决方案 Simple coding bug...comments added to your code to explain issue
if (tollResponse == "yes")
{
Console.WriteLine("How much do you pay per trip?");
string tollTax = Console.ReadLine();
//toll.Success gets set here.
Match toll = Regex.Match(tollTax, @"[\d .]+");
if (toll.Success)
{
//Not sure why you are doing this since you aren't using it in the given code
Math.Round(Convert.ToDecimal(tollTax), 2);
Console.WriteLine("Good lord that's high... well it's your money");
}
else
{
//This is an infinite loop because toll.Success is never set again.
do
{
Console.WriteLine("Please enter a proper number");
tollTax = Console.ReadLine();
} while (toll.Success == false);
}
}
What i think you want
if (tollResponse == "yes")
{
Console.WriteLine("How much do you pay per trip?");
//Loop over the Console.ReadLine() using the else statement and exit if it is right the first time
do
{
string tollTax = Console.ReadLine();
//toll.Success gets set here.
Match toll = Regex.Match(tollTax, @"^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$");
if (toll.Success)
{
Console.WriteLine("Good lord that's high... well it's your money");
//will exit
}
else
{
Console.WriteLine("Please enter a proper number");
}
} while (toll.Success == false);
}
Note: Removed 1 line of duplicate code as well, updated to use my recommended regex and removed Math.Round
Regex Tool
[\d .]+
A more Valid Regex for currency
decimal optional (two decimal places)
^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$
Explained:
number (decimal required)
^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}$
Options: case insensitive
Assert position at the beginning of the string «^»
Match a single character present in the list below «[+-]?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
The character "+" «+»
The character "-" «-»
Match a single character in the range between "0" and "9" «[0-9]{1,3}»
Between one and 3 times, as many times as possible, giving back as needed (greedy) «{1,3}»
Match the regular expression below «(?:,?[0-9]{3})*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character "," literally «,?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single character in the range between "0" and "9" «[0-9]{3}»
Exactly 3 times «{3}»
Match the character "." literally «\.»
Match a single character in the range between "0" and "9" «[0-9]{2}»
Exactly 2 times «{2}»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
Will Match:
1,432.01
456.56
654,246.43
432
321,543
14239729
21379312.32
Will not Match
324,123.432
,,,312,.32
123,.23
taken from my answer here php - regex - how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01)?
这篇关于在do ... while中将相关表达式匹配设置为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文