双条件While循环,其中一个结束程序

时间:2016-08-03 01:24:13

标签: c# while-loop readkey

当用户输入'q'

时,我有一些代码退出程序
//press 'q' to quit application
ConsoleKeyInfo info = Console.ReadKey(true); ;
while (info.KeyChar != 'q') {
    info = Console.ReadKey(true);
}

如何修改此结构,以便在捕获的密钥为“p”时会出现不同的非终止行为?

如果我将条件更改为:

(info.KeyChar != 'q') && (info.KeyChar != 'p')

然后'p'也将终止程序。即使我在while循环中放置逻辑来处理'p'情况。

此外:

ConsoleKeyInfo info = Console.ReadKey(true);
while (true) {
    info = Console.ReadKey(true);
    if (info.KeyChar == 'q') break;
    else if (info.KeyChar == 'p') {
        //other behavior
     }
}

要求用户按两次'q'以结束程序由于某种原因,但预期的行为是一键按下触发操作。

7 个答案:

答案 0 :(得分:3)

var exitWhile = false;
while (!exitWhile) {
    ConsoleKeyInfo info = Console.ReadKey(true);
    switch (info.KeyChar) {
        case 'q':
            exitWhile = true;
            break;

        case 'p':
            //something else to do
    }
}

答案 1 :(得分:1)

因为你两次调用了ReadKey,试试这个:

while (true) {
    var info = Console.ReadKey(true);
    if (info.KeyChar == 'q') break;
    else if (info.KeyChar == 'p') {
        //other behavior
    }
}

答案 2 :(得分:0)

就个人而言,我会选择这样的事情:

ConsoleKeyInfo info;
bool done = false;
while (!done) {
    info = Console.ReadKey(true);
    switch(info.KeyChar) {
        case 'p':
            // do something
            break;
        case 'q':
            done = true;
            // do something else
            break;
    }
}

答案 3 :(得分:0)

do {
   info = Console.ReadKey(true);
   if (info.KeyChar == 'q') break;
   else if (info.KeyChar == 'p') {
    //other behavior
   }
}while (true);

答案 4 :(得分:0)

while (true)
{
    ConsoleKeyInfo info = Console.ReadKey(true);
    {
        if (info.KeyChar == 'q')
            Environment.Exit(0);
        else if (info.KeyChar == 'p')
        {
            //other behavior
        }
    }
}

答案 5 :(得分:0)

ConsoleKeyInfo info = Console.ReadKey(true);
while (info.KeyChar != 'q') {
    if (info.KeyChar != 'p') {
        // handle p case
    }
    info = Console.ReadKey(true);
}

答案 6 :(得分:0)

你可以用一些更具功能性的编程风格来编写你的程序。我假设你想要做一些比退出更复杂的事情,所以我提出的模式对于更基本的场景来说是过度的。这里的要点是,您应该能够用更少的代码指定所有操作,几乎所有操作都集中在一个地方。并且运行的方法应该是描述性的 - 几乎是自我记录的。

// Define a class that can return some basic information about what should happen next
// This only has one property but could be much richer
public class KeyActionResult {
   public KeyActionResult(bool shouldQuit) {
      ShouldQuit = false;
   }
   public bool ShouldQuit { get; }
   // put other return values or information
}

public static class MyProgram {
   // Doing this in advance requires the class to be immutable
   private static KeyActionResult _shouldNotQuit = new KeyActionResult(false);
   private static KeyActionResult _shouldQuit = new KeyActionResult(true);

   // Work up the vocabulary that you want to be able to use
   private static Action<KeyActionResult> KeepLoopingAfter(Action action) =>
      () => {
         action();
         return _shouldNotQuit;
      };

   private static Action<KeyActionResult> QuitAfter(Action action) =>
      () => {
         action();
         return _shouldQuit;
      };

   private Action<KeyActionResult> Quit() => () => _shouldQuit;

   private void DoSomethingComplicated() { /* whatever */ }

有了这些,您可以使用它们来真正清理代码。

   // Bind keys to vocabulary and actions, all in high-signal code
   private static keyActions = new Dictionary<char, Action<KeyActionResult>> {
      ['p'] = KeepLoopingAfter(() =>  Console.WriteLine("You pressed 'p'!")),
      ['d'] = KeepLoopingAfter(DoSomethingComplicated),
      ['q'] = Quit(),
      ['x'] = QuitAfter(() => Console.WriteLine("Delete, then quit!"))
   }
      .AsReadOnly();

   // Run the main decision loop
   public static void Main() {
      bool shouldQuit;
      do {
         ConsoleKeyInfo keyInfo = Console.ReadKey(true);
         Action action;
         if (keyActions.TryGetValue(keyInfo.KeyChar, out action)) {
            shouldQuit = action().ShouldQuit;
         } else {
            shouldQuit = false;
         }
      } while (!shouldQuit)
   }
}

您可以开始进行多播,将其提升到新的水平......