How do I get the console application to start until the user enters "Q", "q", "Quit" or "quit" to complete it?

How to create a console application until the user logs in to Q, q, Quit, or quit to complete it?

This is my current code:

public class Class1 { [STAThread] static void Main(string[] args) { string userName; int i = 0, totalCal = 0, cal = 1; Console.WriteLine("Welcome to the magical calorie counter!"); Console.WriteLine(); Console.Write("Enter in your name -> "); userName = Console.ReadLine(); for (i = 0; i <= 10; i++) { Console.WriteLine("Hello {0}.....Let add some calories!!", userName); }// end for loop Console.WriteLine(); while (cal != 0) { Console.Write("Enter some Calories:<or 0 to quit>: "); cal = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("You entered {0}", cal); Console.WriteLine(); totalCal += cal; Console.WriteLine(); Console.WriteLine("-------------------So far you have a total of {0}------------------", totalCal); Console.WriteLine(); Console.WriteLine(); }// end of while }// end of amin }//end of class 
+4
source share
3 answers

Assuming there are no other errors in your program ... just add a couple of lines like this:

 using System; class Class1 { [STAThread] static void Main(string[] args) { string userName; string line; int i = 0, totalCal = 0, cal = 1; Console.WriteLine("Welcome to the magical calorie counter!"); Console.WriteLine(); Console.Write("Enter in your name -> "); userName = Console.ReadLine(); for (i = 0; i <= 10; i++) { Console.WriteLine("Hello {0}.....Let add some calories!!", userName); }// end for loop Console.WriteLine(); while (true) { Console.Write("Enter some Calories:<or 0 to quit>: "); line = Console.ReadLine().ToLower(); if (line == "q" || line == "quit") { break; } else if (!int.TryParse(line, cal)) { Console.WriteLine("Not a valid option. Please try again."); continue; } Console.WriteLine("You entered {0}", cal); Console.WriteLine(); totalCal += cal; Console.WriteLine(); Console.WriteLine("-------------------So far you have a total of {0}------------------", totalCal); Console.WriteLine(); Console.WriteLine(); }// end of while }// end of amin } 
+5
source

I think quitting, or stopping, is good. Or you can accept e or q.

0
source

I would prefer that a keyboard with knowledge of the character can use the keyboard, such as Ctrl + W, which is the default key for closing tabs in modern web browsers.

-1
source

All Articles