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:
- Initializes a random number (see code fragment below) to a value between 1 and 10, inclusive.
- Repeatedly asks the user to guess what the number is until the number is guessed.
- If the guess is higher than the number, tell the user their guess is too high and that they should guess again.
- If the guess is lower than the number, tell the user their guess is too low and that they should guess again.
- If the guess is equal to the number, tell them:
- That their guess was correct, and
- 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
Get step-by-step solutions from verified subject matter experts
