【问题标题】:While loop in C# going infiniteC#中的while循环无限循环
【发布时间】:2013-02-22 19:08:19
【问题描述】:

下面是我的代码

namespace ProgrammingTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("GUESS THE ANSWER");
            Console.WriteLine("Please enter the input");

            int input1 = (int.Parse(Console.ReadLine()));
            while (input1 != 4)
            {
                Console.WriteLine("Try agian");
            }
            if (input1 == 4)
            {
                Console.WriteLine("You are a winner");
                Console.ReadLine();
            }
            else if (input1 < 4)
            {
                Console.WriteLine("TOOOOO low");
                Console.ReadLine();
            }
            else if (input1 > 4)
            {
                Console.WriteLine("TOOOO high");
                Console.ReadLine();          
            }
        }
    }
}

我在这里设置了 while 条件,但仍然无法继续。它正在无限循环

在这里我想应用一个条件,如果用户输入的数字不是 4,那么它应该开始并且用户应该再次输入数字。现在它正在关闭控制台应用程序。如何放置while循环

【问题讨论】:

  • 您也必须在循环中使用ReadKey,并检查4 和中断等。当您在循环中执行WriteLine 时,它不仅仅是备份到int intpu1... 行。它只是继续前进,因为input1 总是 4。

标签: c# while-loop console-application


【解决方案1】:

您只是在循环开始之前更改input1 的值。由于您永远不会再更改它,因此它将永远循环下去。

编辑:您可能需要考虑为您的循环执行以下操作:

int input1 = 0;
while (input1 != 4)
      {
        input1 = (int.Parse(Console.ReadLine()));

        if (input1 == 4)
        {
            Console.WriteLine("You are a winner");
        }
        else if (input1 < 4)
        {
            Console.WriteLine("TOOOOO low");
          Console.WriteLine("Try agian");

        }
        else if (input1 > 4)
        {
            Console.WriteLine("TOOOO high");
          Console.WriteLine("Try agian");

        }    
}

【讨论】:

  • 它没有发生,当我输入 4 以外的数字时它正在执行,但它应该完成该过程,直到我完成正确的猜测,即 4
【解决方案2】:

添加

input1 = (int.Parse(Console.ReadLine()));

在您的 while 循环结束时,否则 input1 将永远不会更改。

但这还不够。

你应该这样做(不是很好,但工作)

static void Main(string[] args)
{
    Console.WriteLine("GUESS THE ANSWER");


    int answer;
    var success = false;
    Console.WriteLine("Please enter the input");
    while (!success) {

       var userInput = Console.ReadLine();

       if (!Int32.TryParse(userInput, out answer))
          Console.WriteLine("You must enter an integer");

       else {
          if (answer < 4)
             Console.WriteLine("Too low");

          else if (answer > 4)
             Console.WriteLine("Too high");

          else {
             Console.WriteLine("Grats !");
             success = true;
          }
        }
      if (!success)
         Console.WriteLine("Try again !");
     }
}

【讨论】:

    【解决方案3】:

    我修改了你的代码,请检查

         namespace ProgrammingTesting
    {
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("GUESS THE ANSWER");
            Console.WriteLine("Please enter the input");
            int input1 = 0;
            while (input1 != 4)
            {
                input1 = (int.Parse(Console.ReadLine()));
    
    
                if (input1 == 4)
                {
                    Console.WriteLine("You are a winner");
                    Console.ReadLine();
                }
                else if (input1 < 4)
                {
                    Console.WriteLine("TOOOOO low");
    
    
                }
                else if (input1 > 4)
                {
                    Console.WriteLine("TOOOO high");
    
                }
            }
        }
    }
    

    }

    去掉 Console.Readline() ,这样就不用输入 doble 时间了

    【讨论】:

      【解决方案4】:

      好吧,input1 在循环内的任何地方都不会改变。所以,很自然,如果循环进入一次,那么每次都进入。

      【讨论】:

        【解决方案5】:
          while (input1 != 4)
          {
              Console.WriteLine("Try agian");
        
          }
        

        在上面的代码中,input1 的值永远不会改变,所以它会永远循环

        【讨论】:

          【解决方案6】:

          我更正了您的缩进,您可以看到您的 while 块仅包含 Console.WriteLine 而不是输入检查,因此 input1 不会更改并且您的循环是无限的。

          如果您删除 Console.WriteLine 下的 } 并将其放在末尾,您将能够通过输入 4 来停止循环。

          【讨论】:

            【解决方案7】:

            if (input1 == 4) 正上方有一个额外的 } 正在关闭 while 循环,因此 while 循环永远不会退出,因为它内部没有代码。将大括号向下移动到 if else 下方。

            【讨论】:

            • -1。即使他进行了这些更改,input1 的值也不会改变,并且循环仍然是无限的。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-03-29
            • 2016-08-27
            • 2015-04-29
            • 1970-01-01
            • 2015-06-25
            • 2012-12-24
            • 2011-11-19
            相关资源
            最近更新 更多