问题
I am trying to create a program where the application should never exit until I press option '6'. Below is my sample program: Right now, if I press any option, it executes the corresponding method and console window closes (exits the application) after it is done executing. I want the console to wait for the next option to be entered.
Console.WriteLine(@"Select one of the following option:
1-Write Apps
2-Write Drivers
3-Write OS
4-Write Packages
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
return;
case 2:
DoDrivers(reqObj);
return;
case 3:
DoOS(reqObj);
return;
case 4:
DoPackages(reqObj);
return;
case 5:
DoAll(reqObj);
return;
case 6:
Environment.Exit(0);
return;
default:
DoAll(reqObj);
return;
}
回答1:
using System;
namespace LoopUntilSelectionTester
{
class Program
{
static void Main()
{
string key;
while((key = Console.ReadKey().KeyChar.ToString()) != "6")
{
int keyValue;
int.TryParse(key, out keyValue);
ProcessInput(keyValue);
}
}
private static void ProcessInput(int keyValue)
{
switch (keyValue)
{
case 1:
DoApps(reqObj);
break;
case 2:
DoDrivers(reqObj);
break;
case 3:
DoOS(reqObj);
break;
case 4:
DoPackages(reqObj);
break;
case 5:
DoAll(reqObj);
break;
}
}
}
}
回答2:
You can just place your code into a loop, and change the return
to break
statements:
while(true) // Loop forever
{
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
break; // Break, don't return
case 2:
DoDrivers(reqObj);
break;
case 3:
DoOS(reqObj);
break;
case 4:
DoPackages(reqObj);
break;
case 5:
DoAll(reqObj);
break;
case 6:
Environment.Exit(0);
break;
default:
DoAll(reqObj);
break;
}
}
If you wish to prompt for input each iteration, you can move the prompt into the loop, as well.
回答3:
First, what I'd do is make it into a method:
public void AskForInput()
{
Console.WriteLine(@"Select one of the following options:
1-Write Apps
2-Write Drivers
3-Write OS
4-Write Packages
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
return;
case 2:
DoDrivers(reqObj);
return;
case 3:
DoOS(reqObj);
return;
case 4:
DoPackages(reqObj);
return;
case 5:
DoAll(reqObj);
return;
case 6:
Environment.Exit(0);
return;
default:
DoAll(reqObj);
return;
}
}
and then just use it like this:
for (;;) AskForInput();
回答4:
Just wrap the whole thing in a while statement.
while (true)
{
Console.WriteLine(@"Select one of the following option:
1-Write Apps
2-Write Drivers
3-Write OS
4-Write Packages
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
break;
case 2:
DoDrivers(reqObj);
break;
case 3:
DoOS(reqObj);
break;
case 4:
DoPackages(reqObj);
break;
case 5:
DoAll(reqObj);
break;
case 6:
Environment.Exit(0);
break;
default:
DoAll(reqObj);
break;
}
}
回答5:
This worked for me:
Console.WriteLine(@"Select one of the following option:
1-Write Applications XML
2-Write Drivers XML
3-Write Operating Systems XML
4-Write Packages XML
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
while (true)
{
switch (selectionKey)
{
case 1:
DoApplications(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 2:
DoDrivers(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 3:
DoOperatingSystems(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 4:
DoPackages(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 5:
DoAll(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 6:
Environment.Exit(0);
break;
default:
DoAll(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
}
}
来源:https://stackoverflow.com/questions/17223063/continuous-input-to-switch-case-until-i-press-exit-c-sharp