简体   繁体   中英

While Loop not looping when condition is not true C#

I am writing a C# console program as part of my assignment. 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; 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());
        }           

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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