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

4

如何使控制台应用程序一直运行,直到用户输入“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

Ctrl + C是迄今为止最好的 :) - leppie
3
尊敬的先生/女士,我想要停止使用这个电脑程序。请立即停止该程序中的所有活动。感谢您对此事的考虑。此致,Z 敬上。 - Anthony Pegram
2
我是唯一一个理解这个问题的人吗?他想知道如何通过输入其中一个命令来使程序退出。 - NullUserException
没错,我只是没有解释清楚。对于造成的困惑很抱歉。 - Chris
@NullUserException - 直到你指出来,我才理解。 - ChaosPandion
显示剩余2条评论
3个回答

7
假设您的程序中没有其他错误...只需添加几行代码,如下所示:
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
}

啊啊啊,开始错了一个循环,但是我明白它的工作原理了。谢谢 Mark。 - Chris
然后将其改为while(true)。+1或勾选标记会很好。 - mpen
马克,我需要15个声望才能给你打勾,还有其他的方法吗? - Chris
@Chris:真的吗?那有点傻...好吧,我刚给了你10分,所以..我猜你得等另外的2分:p填写你的个人资料可以获得100分。 - mpen

0

我认为退出或结束都可以。或者你可以接受输入e或q。


-1

我更喜欢键盘快捷键,这对于键盘熟练的用户来说可能已经很熟悉了,例如Ctrl+W,在现代网络浏览器中是关闭选项卡的默认键。


2
我更喜欢控制台应用程序像控制台应用程序一样运行,而不是像Firefox或Chrome或其他应用程序。键盘快捷键不仅难以输入和记忆,而且在这样的程序中没有意义,并增加了输入的复杂性。 - cHao

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接