简体   繁体   English

条件不成立时While循环不循环C#

[英]While Loop not looping when condition is not true C#

I am writing a C# console program as part of my assignment. 我正在编写C#控制台程序,这是我的作业的一部分。 What I am trying to do here is to display the message each time when the user enters something invalid. 我要在这里做的是每次用户输入无效内容时都显示该消息。 However when I type in something invalid after making the right choice it only displays the error message once. 但是,当我做出正确选择后输入无效内容时,它只会显示一次错误消息。 After the message I make a valid selection and then an invalid selection it exits the console. 消息后,我做出一个有效选择,然后做出一个无效选择,它退出控制台。 I also want it to display the right message whenever the user inputs valid numbers but that does not happen continuously after making an invalid selection. 我还希望它在用户输入有效数字时显示正确的消息,但是选择无效后不会连续发生。 It just exits the console... I tried using "||" 它只是退出控制台...我尝试使用“ ||” instead of "&&" which does not work at all. 而不是根本不起作用的“ &&”。

I am an absolute Beginner in programming so if there is anything I am doing wrong please help me with it. 我是编程的绝对入门者,所以如果我做错了任何事情,请帮助我。

Console.WriteLine("Enter 1, 2, 3, OR 4");
        uI = int.Parse(Console.ReadLine());

        while (uI != 1 && uI != 2 && uI != 3 && uI != 4)
        {
            Console.WriteLine("SELECT 1, 2, 3, OR 4");
            uI = int.Parse(Console.ReadLine());
        }
        if (uI == 1)
        {
            Console.WriteLine("msg");
            Console.WriteLine("SELECT 1, 2, 3, OR 4");
            uI = int.Parse(Console.ReadLine());
        }
        if (uI == 2)
        {
            Console.WriteLine("msgg");
            Console.WriteLine("SELECT 1, 2, 3, OR 4");
            uI = int.Parse(Console.ReadLine());
        }
        if (uI == 3)
        {
            Console.WriteLine("msggg");
            Console.WriteLine("SELECT 1, 2, 3, OR 4");
            uI = int.Parse(Console.ReadLine());
        }
        if (uI == 4)
        {
            Console.WriteLine("msgggg");
            Console.WriteLine("SELECT 1, 2, 3, OR 4");
            uI = int.Parse(Console.ReadLine());
        }

Thank you 谢谢

You need an infinitive loop. 您需要无限循环。 Moreover I'd use a switch case which is more readable: 此外,我将使用更易读的开关盒:

Console.WriteLine( "Enter 1, 2, 3, OR 4" );

while( true ) {
   uI = int.Parse( Console.ReadLine( ) );

   string message = "Error";
   switch( uI ) {
      case 1: message = "msg"; break;
      case 2: message = "msgg"; break;
      case 3: message = "msggg"; break;
      case 4: message = "msgggg"; break;
      default: break;
   }

   Console.WriteLine( message );      
   Console.WriteLine( "SELECT 1, 2, 3, OR 4" );
}

Try this: 尝试这个:

class Program
{
    static void Main(string[] args)
    {
        do
        {
            Console.WriteLine("SELECT 1, 2, 3, OR 4");
            var uI=int.Parse(Console.ReadLine());
            if (uI==1)
            {
                Console.WriteLine("msg");
            } else if (uI==2)
            {
                Console.WriteLine("msgg");
            }
            else if (uI==3)
            {
                Console.WriteLine("msggg");
            }
            else if (uI==4)
            {
                Console.WriteLine("msgggg");
            }
            else
            {
                break;
            }
        } while (true);
    }
}

So the program takes an input and acts upon it, unless it is not 1,2,3, or 4 where it exists the loop with the break; 因此,程序将接受输入并对其执行操作,除非它不是1,2,3或4所在的地方,且带有break;的循环存在break; statement. 声明。

        Console.WriteLine("Enter 1, 2, 3, OR 4");
        uI = int.Parse(Console.ReadLine());
        while(uI > 0) //exit loop if 0 is entered
        {
           switch(uI)
           {
              case 1: Console.WriteLine("msg"); break;
              case 2: Console.WriteLine("msgg"); break;
              case 3: Console.WriteLine("msggg"); break;
              case 4: Console.WriteLine("msgggg"); break;
              default: break; //do not write to console just loop again

           }
           Console.WriteLine("Enter 1, 2, 3, OR 4");
           uI = int.Parse(Console.ReadLine());
        }           

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

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