繁体   English   中英

执行循环,直到用户按下特定按钮

[英]Execute a loop until user presses a specific button

我正在尝试创建一个多功能程序。 一方面,一个数组填充有随机数。 然后,用户输入一个数字,程序返回该数字出现在数组中的哪个位置。 它还返回该数字出现在数组中的次数。

但是,它仅执行一次,然后结束程序。 我希望它提示用户输入要搜索的数字,直到用户按下按钮为止,例如说“ P”。 结果显示后,一旦用户按“ P”,程序应关闭。 关于应使用哪种方法或功能的任何提示?

这是我的代码的细分版本。

Console.Write("Now enter a number to compare: ");
int c = Convert.ToInt32(Console.ReadLine());

for (int j = 0; j < arr.Length; j++)
{
    if (arr[j] == c)
    {
        pos.Add(j);
    }
}          

if (pos.Count == 0)
{
    Console.WriteLine("Sorry this number does not match");
}
else
{
   Console.WriteLine("The number {0} appears {1} time(s)",c,pos.Count);
}

Console.ReadLine();

这应该给您一些入门

您必须在代码周围使用循环并检查要退出的关键字

class Program
{
    static void Main(string[] args)
    {
        var arr = new int[50];
        var pos = new List<int>();
        string result;
        do
        {
            Console.Write("Now enter a number to compare: ");
            result = Console.ReadLine();

            int c;

            if (int.TryParse(result, out c))
            {
                for (int j = 0; j < arr.Length; j++)
                {
                    if (arr[j] == c)
                    {
                        pos.Add(j);
                    }
                }

                if (pos.Count == 0)
                {
                    Console.WriteLine("Sorry this number does not match");
                }
                else
                {
                    Console.WriteLine("The number {0} appears {1} time(s)", c, pos.Count);
                }
            }


        } while (result != "exit");
    }
}

我将提供另一种方法,但是没有测试下面的代码。

class Program
{

    //declare your class variables here
    //so that you can access them from the methods and do your operations
    bool Up=true;

    static void Main(string[] args)
    {
        Console.WriteLine("Program started.");

        ThreadPool.QueueUserWorkItem(ConsoleCommands);
        while (Up)
        {
            Thread.Sleep(2000);
        }
        Console.WriteLine("Program ended.");
    }

    private static void ConsoleCommands(object dummy)
    {
        while (Up)
        {
            string cmd = ConsoleReceiver().ToLower();
            switch (cmd)
            {
                case "exit":
                    Up=false;
                    break;
                //implement more cases here and fill the rest of your business
                //example:
                case "1":
                    if (pos.Count == Int32.Parse(cmd))//just a dummy business
                    {
                        Console.WriteLine("Sorry this number does not match");
                    }
                    else//another dummy business
                    {
                        Console.WriteLine("Sth...");
                    }
                    break;
                default:
                    Console.WriteLine("Unrecognized command");
                    break;
            }//or forget about switch and use if-else stements instead.
        }
    }

    private static string ConsoleReceiver()
    {
        Console.WriteLine("#cmd:");
        return Console.ReadLine();
    }
}

如果要显式读取单键笔触...

ConsoleKeyInfo keyInfo;
do {
    Console.Write("Enter a number to compare; press the 'p' key to quit: ");
    keyInfo = Console.ReadKey(false);

    int c;
    if (Int32.TryParse(keyInfo.KeyChar.ToString(), out c))
    {    
        for (int j = 0; j < arr.Length; j++)
        {
            if (arr[j] == c)
            {
                pos.Add(j);
            }
        }          

        if (pos.Count == 0)
        {
            Console.WriteLine("Sorry this number does not match");
        }
        else
        {
           Console.WriteLine("The number {0} appears {1} time(s)",c,pos.Count);
        }
} while (keyInfo.Key != ConsoleKey.P)

否则,您可以结合@Fredou和我发布的内容来发挥创意。

尝试这个 :)

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            start:
            string tryagain;


           //All of your Code Goes here


            tryagain = Console.ReadLine();
            if (tryagain != "p")
            {
            goto start;    
            }

            else
            {
                Environment.Exit(0);
            }


        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM