Question: Guess a number with C# My code doesn't seem to work and I'm not sure how to start the Random Number. Code has to: Initializes

Guess a number with C#

My code doesn't seem to work and I'm not sure how to start the Random Number.

Code has to:

  1. Initializes a random number (see code fragment below) to a value between 1 and 10, inclusive.
  2. Repeatedly asks the user to guess what the number is until the number is guessed.
    1. If the guess is higher than the number, tell the user their guess is too high and that they should guess again.
    2. If the guess is lower than the number, tell the user their guess is too low and that they should guess again.
    3. If the guess is equal to the number, tell them:
      1. That their guess was correct, and
      2. How many guesses they made.

The starting code is:

Random RandomClass = new Random(); int randomNumber; randomNumber = RandomClass.Next(MIN, MAX); //random number will be >= MIN and < MAX

using System; using static System.Console;

namespace GuessGame { class Program

{ static void Main(string[] args) { int i; Random RandomClass = new Random(); int randomNum; randomNum = RandomClass.Next(1,10); do { Write("Enter a number between 1 and 10: "); int.TryParse(ReadLine(), out i);

if (i < randomNum) { WriteLine("Number is less than correct number"); ReadLine(); i++; } if (i > randomNum) { WriteLine("Number is bigger than correct number"); ReadLine(); i++; } if (i == randomNum) { WriteLine("Congratulations! \t You Guess!"); ReadLine(); } else { WriteLine("Invalid try another number"); } WriteLine(" Do you want to play again? Type 'Y'"); } while (ReadLine().ToUpper() == "Y"); WriteLine("Press any key to coninue..."); ReadKey(); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!