【问题标题】:How do I make an console application run until the user enters "Q", "q", "Quit" or "quit" to terminate it?如何让控制台应用程序运行直到用户输入“Q”、“q”、“Quit”或“quit”来终止它?
【发布时间】:2010-09-17 04:31:16
【问题描述】:

如何让控制台应用程序运行直到用户输入“Q”、“q”、“Quit”或“quit”来终止它?

这是我当前的代码:

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's 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

【问题讨论】:

  • 我建议你问这个:programmers.stackexchange.com
  • Ctrl + C 是迄今为止最好的:)
  • “尊敬的先生和/或女士,我分别希望停止使用此计算机程序。我要求从此程序中的所有活动停止。感谢您对此事的考虑。你的,Z。”
  • 只有我能理解这个问题吗?他想知道如何通过键入其中一个命令来退出程序。
  • 没错,我只是没有正确解释。很抱歉造成混乱。

标签: c# console-application exit


【解决方案1】:

假设您的程序中没有任何其他错误...只需添加如下几行:

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's 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
}

【讨论】:

  • ahhhh,开始时循环错误,但我知道它是如何工作的。谢谢马克。
  • 然后改为while(true)。 +1 或复选标记会很好。
  • mark,我需要 15 个代表给你一个复选标记,还有其他方法可以吗?
  • @Chris:真的吗?这有点愚蠢......好吧,我只给了你 10 分,所以.. 我想你必须等待其他 2 分:p 填写你的个人资料以获得 100 我认为。
【解决方案2】:

我认为退出或退出很好。或者您可以接受 e 或 q。

【讨论】:

    【解决方案3】:

    我更喜欢精通键盘的用户可能已经知道的键盘快捷键,例如 Ctrl+W,这是现代 Web 浏览器上关闭选项卡的默认键。

    【讨论】:

    • 我更喜欢控制台应用程序的行为类似于控制台应用程序,而不是 Firefox 或 Chrome 或其他任何东西。键盘快捷键不仅仅是打字和记忆的痛苦;它们只是在这样的程序中没有意义,并且增加了输入读取的复杂性。
    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    相关资源
    最近更新 更多