Home > database >  C# - Counter not functioning correctly
C# - Counter not functioning correctly

Time:11-02

I'm writing a program that tests the users patience. If the user immediately enters 'N' or 'n', the console should output "You're no fun!". If the user enters 'Y' once, the console should output "You are impatient". If 'Y' is pressed twice, the console should output "Just a little more". If 'Y' is pressed three times, the console should output "Nice work". If 'Y' is entered 4 times, the console should output "Three was enough! You are truly patient".

Currently the console will print "You are impatient" even if the user immediately enters 'N' or 'n'. I know it's because the counter will still tick up even though the user is choosing to end the program immediately, but I'm not sure how to fix the counter to make the program work.

int count = 0;
        string msg = "";
        char myChar;
        do
        {
            Console.WriteLine("Enter 'Y' to continue, or 'N' to quit.");
            myChar = Console.ReadKey().KeyChar;
            count  ;
        } while (myChar!='N'&&myChar!='n');
        switch (count)
        {
            case 0:
                msg = "You're no fun!";
                break;
            case 1:
                msg = "You are impatient";
                break;
            case 2:
                msg = "Just a little more";
                break;
            case 3:
                msg = "Nice work";
                break;
            case 4:
                msg = "Three was enough! You are truely patient.";
                break;
            default:
                Console.WriteLine("Enter 'Y' to continue, or 'N' to quit.");
                myChar = Console.ReadKey().KeyChar;
                break;
        }
        Console.WriteLine("\n" msg);

CodePudding user response:

could just put counter in an if statement that also checks if the char isn't 'N' OR 'n'

if(myChar!='N'&&myChar!='n'){
   counter  ;
}
  •  Tags:  
  • c#
  • Related