)Write a program that generates a random number between 1 and 10 inclusive. Ask a user to guess the random number, then display a message indicating whether the user’s guess was too high, too low, or correct.
• The message “Sorry – your guess was too low!” should be displayed if the number entered by the user is lower than the random number generated
• The message “Sorry – your guess was too high!” should be displayed if the number entered by the user is higher than the random number generated.
• The message “Congratulations – your guess is correct!” should be displayed if the number entered by the user matched the random number generated.
using System;
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int value = rnd.Next(1, 11);
string str;
int Number = 0;
Console.WriteLine("Please guess a random number");
while (Number != value)
{
str = Console.ReadLine();
if (int.TryParse(str, out Number))
if (Number >= 1 && Number <= 10)
if (Number == value)
Console.WriteLine("Congratulations – your guess is correct!");
else if (Number < value)
Console.WriteLine("Sorry – your guess was too low!");
else if (Number > value)
Console.WriteLine("Sorry – your guess was too high!");
}
}
}
Comments
Leave a comment