By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 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
  1. static void Main(string[] args)
  2.       {
  3.  
  4.          Random randomNumbers = new Random();
  5.          int secretNumber = randomNumbers.Next(1,101);
  6.          double enterNum = 0;
  7.  
  8.          do
  9.          {
  10.  
  11.  
  12.             // pick random number from 1 to 100
  13.  
  14.             Console.Write("{0} ", secretNumber);
  15.  
  16.             //get input
  17.             Console.WriteLine("Please enter a number from 1 to 100 (O to quit)", enterNum);
  18.             enterNum = Convert.ToDouble(Console.ReadLine());
  19.  
  20.          //loop when the generated number is not equal to the number entered by user
  21.          }
  22.          while (secretNumber != enterNum)
  23.          {
  24.  
  25.                while (enterNum != 0)
  26.  
  27.                   //if the random number is less that the number entered by user
  28.                   if   (secretNumber > enterNum)
  29.                   Console.WriteLine("Your guess is too low.", enterNum);
  30.  
  31.                   //if the random number is more than the number entered by the user
  32.                   else if (secretNumber < enterNum)
  33.                   Console.WriteLine("Your guess is too high.", enterNum);
  34.  
  35.  
  36.          }
  37.  
  38.  
  39.             //if the random number is equal to the number entered by user
  40.             if (secretNumber == enterNum)
  41.             {
  42.                Console.WriteLine(" You got it right.");
  43.  
  44.                do
  45.                {
  46.                   //ask to play again
  47.                   string decision;
  48.  
  49.                   Console.Write("Do you want to play again (yes/no)? ");
  50.                   decision = Console.ReadLine();
  51.                }
  52.  
  53.                while (decision != "yes")
  54.                {
  55.                   // Create random number generator
  56.                   Random randomNumbers = new Random();
  57.  
  58.                   //declare variable to store random number
  59.                   int secretNumber;
  60.                   double enterNum;
  61.  
  62.  
  63.                   //initialize variables
  64.                   enterNum = 0;
  65.  
  66.  
  67.                   // pick random number from 1 to 100
  68.                   secretNumber = randomNumbers.Next(1, 101);
  69.                   Console.Write("{0} ", secretNumber);
  70.  
  71.                   //get input
  72.                   Console.WriteLine("Please enter a number from 1 to 100 (O to quit)",enterNum);
  73.                   enterNum = Convert.ToDouble(Console.ReadLine());
  74.  
  75.                }
  76.             }
  77.  
  78.          Console.WriteLine("Thank you for playing.");
  79.  
  80.  
  81.          }
  82.       }
  83.    }
Feb 4 '07 #1
Share this Question
Share on Google+
4 Replies


Motoma
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
  1. do
  2.          {
  3.  
  4.  
  5.             // pick random number from 1 to 100
  6.  
  7.             Console.Write("{0} ", secretNumber);
  8.  
  9.             //get input
  10.             Console.WriteLine("Please enter a number from 1 to 100 (O to quit)", enterNum);
  11.             enterNum = Convert.ToDouble(Console.ReadLine());
  12.  
  13.          //loop when the generated number is not equal to the number entered by user
  14.          }
  15.          while (secretNumber != enterNum)
  16.          {
  17.  
  18.                while (enterNum != 0)
This is also the case for the next do statement
Expand|Select|Wrap|Line Numbers
  1. do
  2.                {
  3.                   //ask to play again
  4.                   string decision;
  5.  
  6.                   Console.Write("Do you want to play again (yes/no)? ");
  7.                   decision = Console.ReadLine();
  8.                }
  9.  
  10.                while (decision != "yes")
  11.                {
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
  1. do
  2.          {
  3.  
  4.  
  5.             // pick random number from 1 to 100
  6.  
  7.             Console.Write("{0} ", secretNumber);
  8.  
  9.             //get input
  10.             Console.WriteLine("Please enter a number from 1 to 100 (O to quit)", enterNum);
  11.             enterNum = Convert.ToDouble(Console.ReadLine());
  12.  
  13.          //loop when the generated number is not equal to the number entered by user
  14.          }
  15.          while (secretNumber != enterNum)
  16.          {
  17.  
  18.                while (enterNum != 0)
This is also the case for the next do statement
Expand|Select|Wrap|Line Numbers
  1. do
  2.                {
  3.                   //ask to play again
  4.                   string decision;
  5.  
  6.                   Console.Write("Do you want to play again (yes/no)? ");
  7.                   decision = Console.ReadLine();
  8.                }
  9.  
  10.                while (decision != "yes")
  11.                {
The compiler is expecting a matching while statement which should terminate with a semicolon
Feb 4 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.