)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.
The user is given up to 3 attempts to guess.
• If the user did not successfully guess the random number after 3 attempts, display: “The random number was X” (X is the random number).
• If the user successfully guesses the random number within 3 attempts, display: “You guessed the number on attempt number X” (X is the attempt count).
using System;
public class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int value = rnd.Next(1, 11);
string str;
int Number = 0;
int attempt = 0;
Console.WriteLine("Please guess a random number");
while (Number != value && attempt < 3)
{
attempt++;
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!");
}
if (attempt == 3 && Number != value)
Console.WriteLine("The random number was " + value);
else
Console.WriteLine("You guessed the number on attempt number " + attempt);
}
}
Comments
Leave a comment