Random random = new Random();
int x = random.Next(0, 11);
Console.WriteLine("Guess a number betweeen 1 and 10");
while (true)
{
int y = int.Parse(Console.ReadLine());
if (y > x)
{
Console.WriteLine("You guessed high !");
}
if (y < x)
{
Console.WriteLine("You guessed low !");
}
if (y == x)
{
Console.WriteLine("You guessed right !");
Console.ReadLine();
break;
}
}
现在我要做的是知道用户猜测的次数并将其存储在变量中,那么如何做呢?
答案 0 :(得分:0)
创建一个计数器,每个循环将计数器增加一个。
static void Main(string[] args)
{
Random random = new Random();
int x = random.Next(0, 11);
Console.WriteLine("Guess a number betweeen 1 and 10");
// create a counter for the total total guessed answers.
int totalQuessed = 0;
bool gameOver = false;
while (!gameOver)
{
int y = int.Parse(Console.ReadLine());
if (y > x)
{
Console.WriteLine($"You guessed high !");
}
if (y < x)
{
Console.WriteLine("You guessed low !");
}
if (y == x)
{
Console.WriteLine("You guessed right !");
gameOver = true;
}
// increase by one
totalQuessed++;
Console.WriteLine("You've guessed: {0} times", totalQuessed);
}
Console.ReadLine();
}