So I wrote this guessing game in C#, and I want the user to be able to type 'y' or 'Y' at the end to restart the game. I've included a "do-while" loop, but for some reason it only repeats this part of the code, instead of the whole program:
1 2 3 4 5 6 7 8 9 10 11 12 13
i++;
Console.WriteLine("Congratulations!");
if (i <= 10)
{
Console.WriteLine("You guessed {0} times! You must've known!", i);
}
else
{
Console.WriteLine("You guessed {0} times! You can do better than that!", i);
}
Console.WriteLine("Play again (Y or N)?");
answer = Convert.ToChar(Console.ReadLine());
i = 0;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Satterwhite_Project_3
{
publicclass GuessingGame
{
privatestatic Random randomNum = new Random();
publicstaticvoid Main(string[] args)
{
int guess, i = 0;
char answer;
int GuessGenerator = randomNum.Next(1, 1000);
Console.WriteLine("Guess a number: ");
guess = Convert.ToInt32(Console.ReadLine());
do
{
while (guess != GuessGenerator)
{
if (guess > GuessGenerator)
{
Console.WriteLine("Too high.");
i++;
}
else
{
Console.WriteLine("Too Low.");
i++;
}
Console.WriteLine("Guess a number: ");
guess = Convert.ToInt32(Console.ReadLine());
}
i++;
Console.WriteLine("Congratulations!");
if (i <= 10)
{
Console.WriteLine("You guessed {0} times! You must've known!", i);
}
else
{
Console.WriteLine("You guessed {0} times! You can do better than that!", i);
}
Console.WriteLine("Play again (Y or N)?");
answer = Convert.ToChar(Console.ReadLine());
i = 0;
}
while ((answer == 'Y') || (answer == 'y'));
}
}
}
The problem is fairly simple. Essential things from your game are outside the do-while loop when they have to be inside. The reason only the part you showed us is being repeated instead of the whole program, is because of this. Once you win the game, "guess" will be equal to "GuessGenerator", right? So the while loop you have at the top of the do-loop will be skipped. The player needs to get a new number and need to be able to start guesing again -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Satterwhite_Project_3
{
publicclass GuessingGame
{
privatestatic Random randomNum = new Random();
publicstaticvoid Main(string[] args)
{
char answer;
do
{
int guess, i = 0;
int GuessGenerator = randomNum.Next(1, 1000); // Has to be inside the do-loop
Console.WriteLine("Guess a number: "); // Has to be inside the do-loop
guess = Convert.ToInt32(Console.ReadLine()); // Has to be inside the do-loop
while (guess != GuessGenerator)
{
if (guess > GuessGenerator)
{
Console.WriteLine("Too high.");
i++;
}
else
{
Console.WriteLine("Too Low.");
i++;
}
Console.WriteLine("Guess a number: ");
guess = Convert.ToInt32(Console.ReadLine());
}
i++;
Console.WriteLine("Congratulations!");
if (i <= 10)
{
Console.WriteLine("You guessed {0} times! You must've known!", i);
}
else
{
Console.WriteLine("You guessed {0} times! You can do better than that!", i);
}
Console.WriteLine("Play again (Y or N)?");
answer = Convert.ToChar(Console.ReadLine());
i = 0;
} while ((answer == 'Y') || (answer == 'y'));
}
}
}
P.S. I know both c++ and c# have if statements, but this is 100% C#