Hi,
I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he guessed right, it asks the user is he/she wants to play again. If the answer is yes, it generates a random number and asks the user to guess the number again. The user can exit if he enters 0. I have created the following code so far but it does not work. I would appreciate any help.
I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he guessed right, it asks the user is he/she wants to play again. If the answer is yes, it generates a random number and asks the user to guess the number again. The user can exit if he enters 0. I have created the following code so far but it does not work. I would appreciate any help.
Code:
static void Main(string[] args) { Random randomNumbers = new Random(); int secretNumber = randomNumbers.Next(1,101); double enterNum = 0; do { // pick random number from 1 to 100 Console.Write("{0} ", secretNumber); //get input Console.WriteLine("Please enter a number from 1 to 100 (O to quit)", enterNum); enterNum = Convert.ToDouble(Console.ReadLine()); //loop when the generated number is not equal to the number entered by user } while (secretNumber != enterNum) { while (enterNum != 0) //if the random number is less that the number entered by user if (secretNumber > enterNum) Console.WriteLine("Your guess is too low.", enterNum); //if the random number is more than the number entered by the user else if (secretNumber < enterNum) Console.WriteLine("Your guess is too high.", enterNum); } //if the random number is equal to the number entered by user if (secretNumber == enterNum) { Console.WriteLine(" You got it right."); do { //ask to play again string decision; Console.Write("Do you want to play again (yes/no)? "); decision = Console.ReadLine(); } while (decision != "yes") { // Create random number generator Random randomNumbers = new Random(); //declare variable to store random number int secretNumber; double enterNum; //initialize variables enterNum = 0; // pick random number from 1 to 100 secretNumber = randomNumbers.Next(1, 101); Console.Write("{0} ", secretNumber); //get input Console.WriteLine("Please enter a number from 1 to 100 (O to quit)",enterNum); enterNum = Convert.ToDouble(Console.ReadLine()); } } Console.WriteLine("Thank you for playing."); } } }
Comment