控制台应用程序,如何制作Console.ReadLine()IF / ELSE语句?

时间:2018-02-06 04:21:04

标签: c# console-application

我正在制作一个名为“Ferna”的工具..我试图在它上面制作不同的命令,但问题是这些“命令”必须按顺序排列,就像我尝试执行命令一样,必须按顺序执行它们。但是我希望它就像它不必按顺序排列所以如果我写“青色”它执行它,它不必等待第5 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();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:6)

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")
    {
      ...
}

答案 1 :(得分:2)

这是旧的switch语句。每隔一段时间就要打破它!如果/ Else工作得那么好。这将循环并检查下一行。

    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;
               }
           }
        }
    }

答案 2 :(得分:0)

使用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键执行下一个命令而不必键入其名称可能不是一个坏主意。