Answer to Question #344741 in C# for miya

Question #344741

)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).


1
Expert's answer
2022-05-27T09:39:50-0400
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);


    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS