两条件 While 循环,其中之一结束程序
Two-condition While loop, one of which ends program
我有一些代码可以在用户键入 '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' 以结束程序,但预期的行为是通过一次按键触发操作。
因为你调用了两次 ReadKey,试试这个:
while (true) {
var info = Console.ReadKey(true);
if (info.KeyChar == 'q') break;
else if (info.KeyChar == 'p') {
//other behavior
}
}
var exitWhile = false;
while (!exitWhile) {
ConsoleKeyInfo info = Console.ReadKey(true);
switch (info.KeyChar) {
case 'q':
exitWhile = true;
break;
case 'p':
//something else to do
}
}
就个人而言,我会选择这样的东西:
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;
}
}
do {
info = Console.ReadKey(true);
if (info.KeyChar == 'q') break;
else if (info.KeyChar == 'p') {
//other behavior
}
}while (true);
while (true)
{
ConsoleKeyInfo info = Console.ReadKey(true);
{
if (info.KeyChar == 'q')
Environment.Exit(0);
else if (info.KeyChar == 'p')
{
//other behavior
}
}
}
ConsoleKeyInfo info = Console.ReadKey(true);
while (info.KeyChar != 'q') {
if (info.KeyChar != 'p') {
// handle p case
}
info = Console.ReadKey(true);
}
你真的可以用一些更具函数式编程风格的代码来增强你的程序。我假设你想做一些比退出更复杂的事情,所以我提出了一个模式,对于更基本的场景来说,这是过大的。这里的要点是您应该能够用更少的代码指定所有操作,几乎所有操作都集中在一个地方。 运行 的方法应该是描述性的——几乎是自我记录的。
// 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)
}
}
您可以开始进行一些多播以将其提升到一个新的水平...
我有一些代码可以在用户键入 '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' 以结束程序,但预期的行为是通过一次按键触发操作。
因为你调用了两次 ReadKey,试试这个:
while (true) {
var info = Console.ReadKey(true);
if (info.KeyChar == 'q') break;
else if (info.KeyChar == 'p') {
//other behavior
}
}
var exitWhile = false;
while (!exitWhile) {
ConsoleKeyInfo info = Console.ReadKey(true);
switch (info.KeyChar) {
case 'q':
exitWhile = true;
break;
case 'p':
//something else to do
}
}
就个人而言,我会选择这样的东西:
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;
}
}
do {
info = Console.ReadKey(true);
if (info.KeyChar == 'q') break;
else if (info.KeyChar == 'p') {
//other behavior
}
}while (true);
while (true)
{
ConsoleKeyInfo info = Console.ReadKey(true);
{
if (info.KeyChar == 'q')
Environment.Exit(0);
else if (info.KeyChar == 'p')
{
//other behavior
}
}
}
ConsoleKeyInfo info = Console.ReadKey(true);
while (info.KeyChar != 'q') {
if (info.KeyChar != 'p') {
// handle p case
}
info = Console.ReadKey(true);
}
你真的可以用一些更具函数式编程风格的代码来增强你的程序。我假设你想做一些比退出更复杂的事情,所以我提出了一个模式,对于更基本的场景来说,这是过大的。这里的要点是您应该能够用更少的代码指定所有操作,几乎所有操作都集中在一个地方。 运行 的方法应该是描述性的——几乎是自我记录的。
// 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)
}
}
您可以开始进行一些多播以将其提升到一个新的水平...