434,668 Members | 1,552 Online
Need help? Post your question and get tips & solutions from a community of 434,668 IT Pros & Developers. It's quick & easy.

# generate random number and compare to user input

 P: 11 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. Expand|Select|Wrap|Line Numbers 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.");              }       }    } Feb 4 '07 #1
4 Replies

 Expert 2.5K+ P: 3,235 This looks like C# code, am I correct? What doesn't work about this code? Are you getting any compiler errors? And next time, please post all of your code in [CODE ] [/CODE ] brackets! Feb 4 '07 #2

 P: 11 Yes, it is C#. It does not like my nest while loops. It thinks it is a statement and expects me to put a semicolon at the end of the while statement. Sorry about the code not being in the code brackets. Didn't know I had to do that. This looks like C# code, am I correct? What doesn't work about this code? Are you getting any compiler errors? And next time, please post all of your code in [CODE ] [/CODE ] brackets! Feb 4 '07 #3

 100+ P: 1,646 Yes, it is C#. It does not like my nest while loops. It thinks it is a statement and expects me to put a semicolon at the end of the while statement. Sorry about the code not being in the code brackets. Didn't know I had to do that. Yes I can see that it would reject this code. The first while statement is being read as the end of the do_while pair so the compiler wants to see a semicolon Expand|Select|Wrap|Line Numbers 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) This is also the case for the next do statement Expand|Select|Wrap|Line Numbers do                {                   //ask to play again                   string decision;                     Console.Write("Do you want to play again (yes/no)? ");                   decision = Console.ReadLine();                }                  while (decision != "yes")                { The compiler is expecting a matching while statement which should terminate with a semicolon Feb 4 '07 #4

 P: 11 How do I fix it? Have been working on it all week and I have lost all objectivity. Yes I can see that it would reject this code. The first while statement is being read as the end of the do_while pair so the compiler wants to see a semicolon Expand|Select|Wrap|Line Numbers 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) This is also the case for the next do statement Expand|Select|Wrap|Line Numbers do                {                   //ask to play again                   string decision;                     Console.Write("Do you want to play again (yes/no)? ");                   decision = Console.ReadLine();                }                  while (decision != "yes")                { The compiler is expecting a matching while statement which should terminate with a semicolon Feb 4 '07 #5