【问题标题】:While loop runs part way through again before exiting C# [closed]While循环在退出C#之前再次运行中途[关闭]
【发布时间】:2020-01-26 03:57:40
【问题描述】:

我现在只有两节课。我正在努力让程序在输入 QUIT 时退出。 此功能有效,但(我相信)while 循环在退出应用程序之前多运行一次Utility.GetCommand()

当键入 QUIT 时,控制台输出“请输入您的命令:”,然后系统生成退出消息。我不知道这是为什么。

这里是主页。

using System;

namespace Application
{
class Program
{
    static void Main(string[] args)
    {
        //Fields
        bool welcome = false;

        while (Utility.Quit == false)
        {
            if (welcome == false)
            {
                Console.WriteLine("Welcome to CSF Scheduler. Please enter a command to get started.");
                Console.WriteLine("Type 'HELP' for a list of commands.");
                welcome = true;
            }
            Utility.GetCommand(Console.ReadLine());
            Utility.HandleCommand(Utility.Command);
        }
    }
}
}

这里是实用程序类

using System;

public abstract class Utility
{
public Utility()
{
}

//Fields
public static string Command { get; private set; }

public static bool Quit { get; private set; }


//Methods
public static void GetCommand(string command)
    {
        Console.Write("Please enter your command: ");
        Command = command;
    }

public static void HandleCommand(string command)
{
    switch (Command)
    {
        case "QUIT":
            Quit = true;
            break;
    default:
    Console.WriteLine("Command not recognized. Please type 'HELP' to see a list of commands.");
        break;
    }
}
}

【问题讨论】:

  • 在执行Console.Write("Please enter your command: ") 之前,您正在执行Console.ReadLine 而不是将string 传递给GetCommand,只需执行Command = Console.ReadLine(); 即可

标签: c# loops while-loop


【解决方案1】:

使用 func 委托从用户输入中读取

using System;

namespace Application
{
class Program
{
    static void Main(string[] args)
    {
        //Fields
        bool welcome = false;

        while (Utility.Quit == false)
        {
            if (welcome == false)
            {
                Console.WriteLine("Welcome to CSF Scheduler. Please enter a command to get started.");
                Console.WriteLine("Type 'HELP' for a list of commands.");
                welcome = true;
            }
            Utility.GetCommand(()=>Console.ReadLine());
            Utility.HandleCommand(Utility.Command);
        }
    }
}

public abstract class Utility
{
    public Utility()
    {
    }   

    //Fields
    public static string Command { get; private set; }

    public static bool Quit { get; private set; }


        //Methods
    public static void GetCommand( Func<string> command)
    {
        Console.Write("Please enter your command: ");
        Command = command();
    }

    public static void HandleCommand(string command)
    {
        switch (Command)
        {
            case "QUIT":
                Quit = true;
                break;
        default:
        Console.WriteLine("Command not recognized. Please type 'HELP' to see a list of commands.");
            break;
        }
    }
}
}

【讨论】:

  • 感谢您的建议。它说字符串不是 lambda 表达式并且不会工作
  • 在您的实用程序以及主要功能中运行更改,它正在工作
  • GetCommand方法也修改了
  • 使用此解决方案,您只需执行Utility.GetCommand(Console.ReadLine),因为Console.ReadLine 匹配Func&lt;string&gt; 代表
  • 哦,我没有看到实用程序的变化,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-20
  • 1970-01-01
  • 2013-03-01
  • 2021-12-02
相关资源
最近更新 更多