我创建了一个程序,要求输入并返回一个值。之后,我想询问用户是否想要继续。但我不知道该用什么。
答案 0 :(得分:3)
您希望使用do
/ while
循环或带有条件while
的无限break
循环。
答案 1 :(得分:2)
经常使用do-while循环:
bool @continue = false;
do
{
//get value
@continue = //ask user if they want to continue
}while(@continue);
在评估循环条件之前,循环将执行一次。
答案 2 :(得分:2)
这只允许2个键(Y和N):
ConsoleKeyInfo keyInfo;
do {
// do you work here
Console.WriteLine("Press Y to continue, N to abort");
do {
keyInfo = Console.ReadKey();
} while (keyInfo.Key != ConsoleKey.N || keyInfo.Key != ConsoleKey.Y);
} while (keyInfo.Key != ConsoleKey.N);
答案 3 :(得分:1)
我会使用do..while
循环:
bool shouldContinue;
do {
// get input
// do operation
// ask user to continue
if ( Console.ReadLine() == "y" ) {
shouldContinue = true;
}
} while (shouldContinue);
答案 4 :(得分:0)
使用Do While
循环。类似的东西会起作用
int input=0;
do
{
System.Console.WriteLine(Calculate(input));
input = GetUserInput();
} while (input != null)
答案 5 :(得分:0)
你可能想要一个while循环,例如:
bool doMore= true;
while(doMore) {
//Do work
//Prompt user, if they refuse, doMore=false;
}
答案 6 :(得分:0)
从技术上讲,任何循环都会这样做,例如for
循环(这是写while(true){;}
的另一种方式)
for (; true; )
{
//Do stuff
if (Console.ReadLine() == "quit")
{
break;
}
Console.WriteLine("I am doing stuff");
}