I made a program that asks for input and returns a value. After, I want to ask if the user wants to continue. But I don't know what to use.
views:
119answers:
7
+3
A:
You want to use a do
/ while
loop, or an infinite while
loop with a conditional break
.
SLaks
2010-03-02 23:18:23
A:
use Do While
Loop. something similar will work
int input=0;
do
{
System.Console.WriteLine(Calculate(input));
input = GetUserInput();
} while (input != null)
Asad Butt
2010-03-02 23:18:34
A:
You probably want a while loop here, something like:
bool doMore= true;
while(doMore) {
//Do work
//Prompt user, if they refuse, doMore=false;
}
Nick Craver
2010-03-02 23:18:44
+2
A:
A do-while loop is often used:
bool @continue = false;
do
{
//get value
@continue = //ask user if they want to continue
}while(@continue);
The loop will be executed once before the loop condition is evaluated.
Lee
2010-03-02 23:18:52
+1
A:
I would use a do..while
loop:
bool shouldContinue;
do {
// get input
// do operation
// ask user to continue
if ( Console.ReadLine() == "y" ) {
shouldContinue = true;
}
} while (shouldContinue);
Lucas Jones
2010-03-02 23:19:55
This one was the easiest to understand. Thanks.
Slateboard
2010-03-03 00:25:23
+2
A:
This only allows 2 keys (Y and 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);
Obalix
2010-03-02 23:24:25
A:
Technically speaking, any loop will do it, a for
loop for example (which is another way to write a while(true){;}
)
for (; true; )
{
//Do stuff
if (Console.ReadLine() == "quit")
{
break;
}
Console.WriteLine("I am doing stuff");
}
martani_net
2010-03-02 23:25:10