【问题标题】:Execute a loop until user presses a specific button执行循环,直到用户按下特定按钮
【发布时间】:2014-09-12 02:42:34
【问题描述】:

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

但是它只执行一次,然后结束程序。我希望它提示用户输入要搜索的数字,直到用户按下按钮,例如“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();

【问题讨论】:

  • 提示:查找while循环。
  • 问题定义和问题都不清楚。请尝试清楚地重新定义您的问题并缩小问题范围。问候,
  • 您似乎掌握了循环的概念(我已经在您的代码中看到了一个)。想想你还可以在哪里应用循环。

标签: c# arrays loops


【解决方案1】:

这应该会给你一个良好的开端

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

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

【讨论】:

    【解决方案2】:

    如果您想显式读取单个按键的笔划...

    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 和我发布的内容来发挥创意。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 您在 Main() 中定义 Up
      【解决方案4】:

      试试这个:)

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

      【讨论】:

      • 因为 goto 而投反对票。使用 goto 从来都不是一个好主意。
      • 好的,我同意在这里使用它不是一个好主意,但是您对在 spaghetticode 中使用 goto 有什么看法?
      • 程序员编写意大利面条代码,这完全取决于您的设计。再说一遍,我永远不会使用 goto,也不会建议别人使用它。
      猜你喜欢
      • 1970-01-01
      • 2022-12-31
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      相关资源
      最近更新 更多