【问题标题】:C# User Input Loop Skipping CodeC# 用户输入循环跳过代码
【发布时间】:2016-02-06 12:59:37
【问题描述】:

对编码非常陌生。我发布了一个关于如何在不导致程序崩溃的情况下检查无效用户输入的问题,这个问题很快得到了回答,谢谢!我现在已经将循环设置为拒绝除 1-10 之间的值以外的任何内容,并试图让它进入下一个要输入的属性,但不知道如何阻止它跳到代码末尾.这是我的代码:

        UInt32 attributePoints = 25;
        bool validInput = false;
        string inputStrength;
        string inputSpeed;
        UInt32 validStrength = 0;
        UInt32 validSpeed = 0;
        while (!validInput)
        {
            Console.WriteLine("You have: " + attributePoints + " attribute points remaining to choose from");
            Console.Write("Please enter a value between 1-10 for strength: ");
            inputStrength = Console.ReadLine();
            if (!UInt32.TryParse(inputStrength, out validStrength))
            {
                Console.WriteLine("Input was not a valid value for strength.");
            }
            else if (validStrength < 0 || validStrength > 10)
            {
                Console.WriteLine("Input was not a valid value for strength.");
            }
            else
            {
                validInput = true;
            }

            while (!validInput)
            {
                Console.Write("Please enter a value between 1-10 for speed: ");
                inputSpeed = Console.ReadLine();
                if (!UInt32.TryParse(inputSpeed, out validSpeed))
                {
                    Console.WriteLine("Input was not a valid value for speed.");
                }
                else if (validSpeed < 0 || validSpeed > 10)
                {
                    Console.WriteLine("Input was not a valid value for speed.");
                }
                else
                {
                    validInput = true;
                }

            }
        Console.WriteLine(String.Format("Strength Value = {0}", validStrength));
        Console.WriteLine(String.Format("Speed Value = {0}", validSpeed));
    }

如果我运行程序故意输入一个无效的速度输入,最终结果是这样的:

请输入一个介于 1-10 之间的强度值:a

输入的强度值无效。

请为速度输入一个介于 1-10 之间的值:5

强度值 = 0

速度值 = 5

我怎样才能让这个代码一直要求强度值,直到给出一个有效的数字,然后继续速度?谢谢!

【问题讨论】:

    标签: c# loops while-loop


    【解决方案1】:

    将您的代码更改为:

        UInt32 attributePoints = 25;
        string inputStrength;
        string inputSpeed;
        UInt32 validStrength = 0;
        UInt32 validSpeed = 0;
        while (true)
        {
            Console.WriteLine("You have: " + attributePoints + " attribute points remaining to choose from");
            Console.Write("Please enter a value between 1-10 for strength: ");
            inputStrength = Console.ReadLine();
            if (!UInt32.TryParse(inputStrength, out validStrength))
                Console.WriteLine("Input was not a valid value for strength.");
            else if (validStrength < 0 || validStrength > 10)
                Console.WriteLine("Input was not a valid value for strength.");
            else
                break;
        }
        while (true)
        {
            Console.Write("Please enter a value between 1-10 for speed: ");
            inputSpeed = Console.ReadLine();
            if (!UInt32.TryParse(inputSpeed, out validSpeed))
                Console.WriteLine("Input was not a valid value for speed.");
            else if (validSpeed < 0 || validSpeed > 10)
                    Console.WriteLine("Input was not a valid value for speed.");
            else
                 break;
        }
        Console.WriteLine(String.Format("Strength Value = {0}", validStrength));
        Console.WriteLine(String.Format("Speed Value = {0}", validSpeed));
    

    【讨论】:

      猜你喜欢
      • 2013-04-23
      • 2017-08-29
      • 2013-03-25
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 2018-09-18
      • 2021-11-29
      • 2016-04-26
      相关资源
      最近更新 更多