1

我有一些代码在用户键入“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”来结束程序,但预期的行为是通过一键触发操作。

4

7 回答 7

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
    }
}
于 2016-08-03T01:30:33.043 回答
1

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

while (true) {
    var info = Console.ReadKey(true);
    if (info.KeyChar == 'q') break;
    else if (info.KeyChar == 'p') {
        //other behavior
    }
}
于 2016-08-03T01:29:49.060 回答
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;
    }
}
于 2016-08-03T01:31:49.277 回答
0
do {
   info = Console.ReadKey(true);
   if (info.KeyChar == 'q') break;
   else if (info.KeyChar == 'p') {
    //other behavior
   }
}while (true);
于 2016-08-03T01:34:09.003 回答
0
while (true)
{
    ConsoleKeyInfo info = Console.ReadKey(true);
    {
        if (info.KeyChar == 'q')
            Environment.Exit(0);
        else if (info.KeyChar == 'p')
        {
            //other behavior
        }
    }
}
于 2016-08-03T01:35:03.380 回答
0
ConsoleKeyInfo info = Console.ReadKey(true);
while (info.KeyChar != 'q') {
    if (info.KeyChar != 'p') {
        // handle p case
    }
    info = Console.ReadKey(true);
}
于 2016-08-03T01:37:10.483 回答
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)
   }
}

你可以开始做一些多播来把它提升到一个新的水平......

于 2016-08-03T01:58:34.467 回答