简体   繁体   English

C# 中的 TryParse 并使用 while 循环

[英]TryParse in C# and using the while loop

The user Input should be within 0 to 10 and I want to use tryparse and check if the input is correct, if not then it should prompt the user again.用户输入应在 0 到 10 之间,我想使用 tryparse 并检查输入是否正确,如果不正确,则应再次提示用户。 my code is like below it only works once but if the user enters the wrong number it again executes.我的代码如下所示,它只工作一次,但如果用户输入错误的数字,它会再次执行。 In this case, where should I put the loop?在这种情况下,我应该把循环放在哪里?


 int number;
            bool True;
            Console.Write("Enter number between 0 t0 10: ");
            True = int.TryParse(Console.ReadLine(), out number);
                while (number < 0 || number > 10)
                {
             while (True)
                    Console.Write("Enter number between 0 t0 10: ");
                    int.TryParse(Console.ReadLine(), out number);

                }

            {
                Console.WriteLine("Please enter the correct number");

            }
            return number;

use do while.使用做而。

int i = 0;

do
{
   Console.Write("Enter number between 0 t0 10: ");
   True = int.TryParse(Console.ReadLine(), out number);

} while (!True);

I'd write similar code to what you already have, but I'd rename True to isValid and use a do / while loop:我会编写与您已有的代码类似的代码,但我会将True重命名为isValid并使用do / while循环:

int number;
bool isValid = false;
do
{
    Console.Write("Enter number between 0 and 10: ");

    isValid = int.TryParse(Console.ReadLine(), out number) 
                  && number >= 0
                  && number <= 10;

    if (isValid)
    {
        Console.WriteLine("Please enter the correct number");
    }
}
while (!isValid);

Better yet, do away with isValid altogether:更好的是,完全取消isValid

int number;
do
{
    Console.Write("Enter number between 0 and 10: ");
    if (int.TryParse(Console.ReadLine(), out number) && number >= 0 && number <= 10)
    {
        break;
    }
    Console.WriteLine("Please enter the correct number");
}
while (true);

Now, if the number is valid, we simply break out of the loop.现在,如果数字是有效的,我们就简单地跳出循环。

PS You'll see that I swaped the number conditions around to make them validity checks instead, so < became >= and > became <= . PS你会看到我交换了数字条件来进行有效性检查,所以<变成了>=并且>变成了<=

Also, don't call a variable something like True .另外,不要将变量称为True Imagine you're reading through a longer method and you come across this line (months after you've written it):想象一下,您正在阅读更长的方法,并且遇到了这一行(在您编写它几个月后):

while (True)

That's an endless loop right?这是一个无限循环吧? Oh no!不好了! It's the variable True , not the constant true .这是变量True ,而不是常量true Now imagine further up you have bool True = false;现在想象一下你有bool True = false; . . If you're reading quickly, you might misunderstand what the code does.如果您阅读得很快,您可能会误解代码的作用。 Even if you're not, you have to put more mental effort into understanding what's happening.即使你不是,你也必须投入更多的精力来理解正在发生的事情。

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

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