控制台应用程序,如何制作console.Readline()如果/else语句



我正在制作一个名为" ferna"的工具。我尝试执行命令,我必须按顺序执行它们。但是我希望它像不一定要按顺序执行第五ReadLine()

这是我的代码:

using System;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            if (Console.ReadLine() == "cmds")
            {
                Console.WriteLine(cmds);
            }
            else if (Console.ReadLine() == "calculator")
            {
                    cal.ShowDialog();
            }
            else if (Console.ReadLine() == "cyan")
            {
                    Console.ForegroundColor = ConsoleColor.Cyan;
            }
            else if (Console.ReadLine() == "black")
            {
                Console.ForegroundColor = ConsoleColor.Black;
            }
            else if (Console.ReadLine() == "clear")
            {
                Console.Clear();
            }
        }
    }
}

Console.ReadLine语句的结果存储在变量中。这样,您不必继续致电Console.ReadLine

var cmd = Console.ReadLine();
if (cmd == "cmds")
{
    Console.WriteLine(cmds);
}
else if (cmd == "calculator")
{
   ...

本质上,问题每次if检查每个条件都将等待更多输入

更新

您需要将其放在loop

string cmd = "";
while(cmd != "exit")
{
    cmd = Console.ReadLine();
    if (cmd == "cmds")
    {
         Console.WriteLine(cmds);
    }
    else if (cmd == "calculator")
    {
      ...
}

这是旧的旧开关语句。必须不时地将其分解!如果/其他工作也是如此。这将循环并检查下一行。

    class Program
    {
        static void Main(string[] args)
        {
         while((string command = Console.ReadLine()) != null)
         {
               switch (command.ToUpper())
                {
                case "CMD":
                    Console.WriteLine("CMD");
                    break;
                case "CALCULATOR":
                     cal.ShowDialog();
                    break;
                default:
                   Console.WriteLine("Default");
                   break;
               }
           }
        }
    }

使用 Queue<string>保留命令列表可能不是一个坏主意:

class Program
{
    static Queue<string> commandQueue = new Queue<string>(new[] {"FirstCommand", "SecondCommand", "ThirdCommand"});
    static void Main(string[] args)
    {
        using (Queue<string>.Enumerator enumerator = commandQueue.GetEnumerator())
        {
            while (enumerator.MoveNext())
            {
                Console.WriteLine("Type in next command or type exit to close application");
                //this while is used to let user make as many mistakes as he/she wants.
                while (true)
                {
                    string command = Console.ReadLine();
                    if (command == "exit")
                    {
                        Environment.Exit(0);
                    }
                    //if typed command equals current command in queue ExecuteCommand and move queue to next one
                    if (command == enumerator.Current)
                    {
                        ExecuteCommand(command);
                        break;
                    }
                    else//Show error message
                    {
                        if (commandQueue.Contains(command))
                        {
                            Console.WriteLine("Wrong command.");
                        }
                        else
                        {
                            Console.WriteLine("Command not found.");
                        }
                    }
                } 
            }
            Console.WriteLine("We are done here, press enter to exit.");
            Console.ReadLine();
        }
    }
    //Method that executes command according to its name
    static void ExecuteCommand(string commandName)
    {
        switch (commandName)
        {
            case "FirstCommand":
                Console.WriteLine("FirstCommand executed");
                break; 
            case "SecondCommand":
                Console.WriteLine("SecondCommand executed");
                break;
            case "ThirdCommand":
                Console.WriteLine("ThirdCommand executed");
                break;
            default:
                Console.WriteLine("Command not found");
                break;
        }     
    }
}

另外,如果要求应以精确顺序执行操作,那么只要让用户按Enter执行下一个命令而不必输入其名称可能不是一个坏主意。

最新更新