James1
James1

Reputation: 283

How to exit or continue in C#

I was wondering if this is simple or complicated, how to exit or continue a small simple app.

Console.WriteLine("Enter an integer: ");
int myInt = Convert.ToInt32(Console.ReadLine());
bool isLessThan10 = myInt < 10;
bool isBetween0And5 = (0 <= myInt) && (myInt <= 5);
Console.WriteLine("Integer less than 10? {0}", isLessThan10);
Console.WriteLine("Integer between 0 and 5? {0}", isBetween0And5);
Console.ReadKey();

How to make it ask a question saying Do you want to quit this or continue and Yes to continue and No to quit.

I don't know to do that, please help!

Thanks

Upvotes: 2

Views: 9173

Answers (3)

Khanh TO
Khanh TO

Reputation: 48972

do {

        //Your Code

        Console.WriteLine("Do you want to continue (Y/N)? "); 
    }while (Console.ReadKey().KeyChar != 'Y');

or

 do {

            //Your Code

            Console.WriteLine("Do you want to continue (Y/N)? "); 
        }while (Console.ReadLine() != "Y");

Upvotes: 4

Vinay Sharma
Vinay Sharma

Reputation: 89

        Console.Write("Press any key to continue or 'n' to abort:");
        if (Console.ReadKey().Key == ConsoleKey.N)
            return;

Upvotes: 1

vyi
vyi

Reputation: 1092

I hope you are not looking for a GUI 'Input Box'( I assume you are doing a console-based application). A simple console message with a keyInput will do.

    char key = 0;
    while( key != 'Q'){

        //Your Code
        //
        Console.WriteLine("Press Q to quit."); 
        key = Console.Readkey();
    }

You can replace the 'While' with 'do-while' as well'

Upvotes: 1

Related Questions