[英]How to run this program repeatedly?
The program runs perfectly for a single attempt, thought I would like to add another feature that will let the user to chose either quite or continue the game repeatedly. 该程序一次尝试即可完美运行,以为我想添加另一个功能,使用户可以选择完全或重复进行游戏。
The issue I am having is that it runs the program and then this line of code Console.WriteLine("Go again? Y/N"); 我遇到的问题是它先运行程序,然后运行以下代码行Console.WriteLine(“ Go again?Y / N”); runs right after asking the user "Enter your guess: ".
在询问用户“输入您的猜测:”后立即运行。 Is there any way to fix this issue so the program runs to the end for as many times as wished for?
有什么办法可以解决此问题,使程序运行到所需次数就可以结束? I tried with a wider while loop too, but it didn't help.
我也尝试了一个更宽的while循环,但没有帮助。
using System; 使用系统;
namespace HiLoGame
{
class Program
{
static void Main(string[] args)
{
bool correct = false;
bool repeat = true;
int guess = 0;
int totalGuess = 0;
Random random = new Random();
int returnNum = random.Next(1, 100);
Console.WriteLine("*** Welcome to the Hi-Lo game ***");
Console.WriteLine("The computer chose a number between 1 and 100, you guess it");
do
{
Console.WriteLine("Enter your guess: ");
guess = int.Parse(Console.ReadLine());
if (guess < returnNum)
{
Console.WriteLine("Your guess is too low!");
totalGuess++;
}
else if (guess > returnNum)
{
Console.WriteLine("Your guess is too high!");
totalGuess++;
}
else if (guess == returnNum)
{
correct = true;
totalGuess++;
Console.WriteLine("*** YOU GOT IT! ***");
Console.WriteLine("Total try: {0}", totalGuess);
}
} while (!correct);
Console.WriteLine("Go again? Y/N");
string go = Console.ReadLine();
if (go == "Y" || go == "y")
{
repeat = true;
}
else
{
repeat = false;
}
}
}
}
Do the same as you did for your gameLoop. 做与您的gameLoop相同的操作。 Short example:
简短示例:
do
{
GameLoop();
Console.WriteLine("Go again? Y/N");
go = Console.ReadLine();
} while (go == "Y" || go == "y");
Full example: 完整示例:
using System;
namespace HiLoGame
{
class Program
{
static void Main(string[] args)
{
string go = string.Empty;
Console.WriteLine("*** Welcome to the Hi-Lo game ***");
Console.WriteLine("The computer chose a number between 1 and 100, you guess it");
do
{
GameLoop();
Console.WriteLine("Go again? Y/N");
go = Console.ReadLine();
} while (go == "Y" || go == "y");
}
private static void GameLoop()
{
bool correct = false;
bool repeat = true;
int guess = 0;
int totalGuess = 0;
Random random = new Random();
int returnNum = random.Next(1, 100);
do
{
Console.WriteLine("Enter your guess: ");
guess = int.Parse(Console.ReadLine());
if (guess < returnNum)
{
Console.WriteLine("Your guess is too low!");
totalGuess++;
}
else if (guess > returnNum)
{
Console.WriteLine("Your guess is too high!");
totalGuess++;
}
else if (guess == returnNum)
{
correct = true;
totalGuess++;
Console.WriteLine("*** YOU GOT IT! ***");
Console.WriteLine("Total try: {0}", totalGuess);
}
} while (!correct);
}
}
}
Try this 尝试这个
class Program
{
static void Main(string[] args)
{
bool repeat = true;
while (repeat)
{
DoTheGame();
Console.WriteLine("Go again? Y/N");
string go = Console.ReadLine();
if (go == "Y" || go == "y")
{
repeat = true;
}
else
{
repeat = false;
}
}
}
public static void DoTheGame()
{
bool correct = false;
int guess = 0;
int totalGuess = 0;
Random random = new Random();
int returnNum = random.Next(1, 100);
Console.WriteLine("*** Welcome to the Hi-Lo game ***");
Console.WriteLine("The computer chose a number between 1 and 100, you guess it");
do
{
Console.WriteLine("Enter your guess: ");
guess = int.Parse(Console.ReadLine());
if (guess < returnNum)
{
Console.WriteLine("Your guess is too low!");
totalGuess++;
}
else if (guess > returnNum)
{
Console.WriteLine("Your guess is too high!");
totalGuess++;
}
else if (guess == returnNum)
{
correct = true;
totalGuess++;
Console.WriteLine("*** YOU GOT IT! ***");
Console.WriteLine("Total try: {0}", totalGuess);
}
} while (!correct);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.