gpt4 book ai didi

c# - 在 do...while 中将相关表达式匹配设置为 false

转载 作者:行者123 更新时间:2023-12-03 20:31:48 24 4
gpt4 key购买 nike

我正在尝试编写一些非常基本的代码,但我也在挑战自己的正则表达式。我已经能够在一定程度上混淆代码,但我真正遇到问题的地方是我试图在表达式为假时运行 do...while 循环。此时我绝对没有收到任何错误,但是 do...while 循环继续运行。

我在下面附上相关代码,希望对您有所帮助。

提前致谢

if (tollResponse == "yes")
{
Console.WriteLine("How much do you pay per trip?");
string tollTax = Console.ReadLine();
Match toll = Regex.Match (tollTax, @"[\d .]+");

if (toll.Success)
{
Math.Round(Convert.ToDecimal(tollTax), 2);
Console.WriteLine("Good lord that's high... well it's your money");
}
else
{
do
{
Console.WriteLine("Please enter a proper number");
tollTax = Console.ReadLine();
}
while (toll.Success == false);
}
}

最佳答案

简单的编码错误...添加到您的代码中以解释问题的注释

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);
}
}

我想你想要什么

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);
}

注意:也删除了 1 行重复代码,更新为使用我推荐的正则表达式并删除了 Math.Round


正则表达式工具

[\d .]+

Regular expression visualization

Debuggex Demo

更有效的货币正则表达式

小数可选(两位小数)

^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$

Regular expression visualization

Debuggex Demo

解释:

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) «$»

将匹配:

1,432.01
456.56
654,246.43
432
321,543
14239729
21379312.32

不会匹配

324,123.432
,,,312,.32
123,.23

取 self 在这里的回答 php - regex - how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01)?

关于c# - 在 do...while 中将相关表达式匹配设置为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24690630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com