我怎样才能将这个goto转换为dowhile循环

本文关键字:转换 goto dowhile 循环 | 更新日期: 2023-09-27 18:36:01

class Program
{
    static void Main(string[] args)
    {
        string choice = string.Empty;
        do
        {
        start:
            int output = 0;
            int number = 0;
            Console.WriteLine("Please input a number for it to be counted!");
            bool conversion = int.TryParse(Console.ReadLine(), out output);
            if (number < 1000)
            {
                switch (conversion)
                {
                    case true:
                        while (number <= output)
                        {
                            Console.Write(number + " ");
                            number += 2;
                        }
                        break;
                    case false:
                        Console.WriteLine("ERROR: INVALID INPUT!");
                        goto start;
                }
            }
            else
            {
                Console.WriteLine("APPLICATION ERROR: NUMBER MUST BE BELOW OR AT 1000 TO PREVENT OVERFLOW!");
                return;
            }
            do // Here is the beginning of the do code
            {
                Console.WriteLine("'n Do you want to continue - Yes or No");
                choice = Console.ReadLine();
                if (choice.ToUpper() != "YES" && choice.ToUpper() != "NO")
                {
                    Console.WriteLine("ERROR INVALID INPUT: Only input Yes or No!");
                }
            } while (choice.ToUpper() != "YES" && choice.ToUpper() != "NO");
        } while (choice.ToUpper() == "YES");
    }
}

我在此语句中使用了几个 do while 循环,但是当用户输入除分配整数的限制以外的任何内容(即放置小数或分数)或他们放置字符串时,我将如何放入循环"错误无效输入:"结果。我只是使用 goto,因为我无法找到将 do while 循环语句放在哪里。如果有人可以简单地向我展示如何用 do while 循环替换那个 goto,那么我会非常伟大。(请注意,如果您向我展示我可以更好地优化代码的方法,因为我仍然是新手,我可能不会理解它,但欢迎您全力以赴!

我怎样才能将这个goto转换为dowhile循环

简短回答:

关键字 continue 表示返回到循环的开头。请注意,这将重新检查循环条件,如果为假,则中断。此外,大多数人发现 do-while 循环的可读性较差(而且它们真的很少是必需的),因此请尝试使用 while 循环。

还有关键字 break 它将简单地退出循环。(不仅适用于开关盒!

更具可读性的版本如下所示:

string userAnswer = "yes";
// while is usually more readable than do-while
while (userAnswer == "yes")
{
    Console.WriteLine("Please input a number for it to be counted!");
    int number;
    // Ask for new input until the user inputs a valid number
    while (!int.TryParse(Console.ReadLine(), out number))
    {
        Console.WriteLine("Invalid number, try again");
    }
    if (number < 1000)
    {
        // Print from 0 to number, jumping in 2's
        for (int i = 0; i <= number; i += 2)
            Console.WriteLine(i + " ");
    }
    else
    {
        Console.WriteLine("APPLICATION ERROR: NUMBER MUST BE BELOW OR AT 1000 TO PREVENT OVERFLOW!");
        continue; // Jump back to the start of this loop
    }
    Console.WriteLine("Continue? (Yes / No)");
    userAnswer = Console.ReadLine().ToLower();
    // Ask for new input until the user inputs "Yes" or "No"
    while (userAnswer != "yes" && userAnswer != "no")
    {
        Console.WriteLine("Invalid input. Continue? (Yes / No)");
        userAnswer = Console.ReadLine().ToLower();
    }
}
嘿,

我会发布一些可能会有所帮助的代码并提供一些建议。基本上声明一个名为"loopComplete"的布尔值,它将继续执行 do while 循环,直到您将其设置为 true。缺点是它不会像返回/转到/等那样立即退出循环,但这在大多数情况下不是问题。

您可能想使用 if/else 而不是 switch(转换),看到它以这种方式完成有点有趣,但它有点过分:)

如果 Int.TryParse() 尚未为分数值返回 false(例如 [15.08] 返回带有 [15] 的 [true])。然后,您可以在使用 TryParse 之前存储 Console.ReadLine(),然后使用 stringName.Contains() 检查"."基本上,如果转换成功,您还可以检查它是否包含小数点。

另一种检查方法是浮点数。TryParse() 然后检查它是否是小数值。

bool fraction = false;
if( number % 1.0f > 0)
    fraction == true;

%称为模量,它返回A/B的余数如果一个数字在除以 1 时有一个余数,它必须是小数值。

    //start:
    bool loopCompleted = false;
    do
    {
        int output = 0;
        int number = 0;
        Console.WriteLine("Please input a number for it to be counted!");
        bool conversion = int.TryParse(Console.ReadLine(), out output);
        if (conversion && number < 1000)
        {
            while (number <= output)
            {
                 Console.Write(number + " ");
                 number += 2;
            }
            loopCompleted = true;
        }
        else
        {
            if(conversion == false)
            {
                 Console.WriteLine("ERROR: INVALID INPUT!");
            }
            else
            {
                 Console.WriteLine("APPLICATION ERROR: NUMBER MUST BE BELOW OR AT 1000 TO PREVENT OVERFLOW!");
            }
        }
   } while(!loopCompleted)