如何循环控制台应用程序

jay*_*t55 5 c# console visual-studio

我只需要能够循环一个控制台应用程序.我的意思是:

program start:
display text
get input
do calculation
display result
display text
get input.

REPEAT PROCESS INFINATE NUMBER OF TIMES UNTIL THE USER EXITS THE APPLICATION.
program end.
Run Code Online (Sandbox Code Playgroud)

我希望这是有道理的.任何人都可以解释我将如何做到这一点?谢谢 :)

Tra*_*Guy 15

Console.WriteLine("bla bla - enter xx to exit");
string line;
while((line = Console.ReadLine()) != "xx")
{
  string result = DoSomethingWithThis(line);
  Console.WriteLine(result);
}
Run Code Online (Sandbox Code Playgroud)


Tib*_*Ana 6

while(true) {
  DisplayText();
  GetInput();
  DoCalculation();
  DisplayResult();
  DisplayText();
  GetInput();
}
Run Code Online (Sandbox Code Playgroud)

用户可以随时停止程序CTRL-C.

这是你的意思吗?


Dan*_*ott 6

您可以在while循环中将main方法的整个主体包装在program.cs中,并始终满足条件.

例如(伪代码)

While (true)
{
   Body
}
Run Code Online (Sandbox Code Playgroud)

善良,


小智 5

使用 While 循环

bool userWantsToExit = false;

get input

while(!userWantsToExit)
{

  do calc;
  display results;
  display text;
  get input;
  if (input == "exit") 
    userWantsToExit = true;
}

program end;
Run Code Online (Sandbox Code Playgroud)