Write a program that generates a random integer in the range of 1 through 10, and asks the users to guess that the number is. If the user’s guess is higher than the random number, the program should display "Too high, try again." If the user’s guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the user, then the program should generate a new random number so the game can start over. Until the users enter 0, quit the program.

Respuesta :

CPED

Answer:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Guessgame

{

   class Program

   {

       static void Main(string[] args)

       {

           //declare variables          

           int random;

           int guess;

           int tries = 0;          

            int quit;

           //Create an instance of Random class

           Random rand = new Random();

           // random number between 1 and 10

           random = rand.Next(10);

           Console.WriteLine("The Guess My Number Game program");

           Console.WriteLine("Enter a guess between 1 and 10");

           guess = Convert.ToInt32(Console.ReadLine());

           while (true)

           {

               //increment the tries by one

               tries++;

             

               //Check if guess is greater than random number

               if (guess > random)

               {

                   //print message

                   Console.WriteLine("Too high, try again.");

                   Console.WriteLine("Enter a guess between 1 and 10");

                   //read guess

                   guess = Convert.ToInt32(Console.ReadLine());

               }

             

               if (guess < random)

               {

                   //print message

                   Console.WriteLine("Too low, try again.");

                   Console.WriteLine("Enter a guess between 1 and 10");

                   //read guess

                   guess = Convert.ToInt32(Console.ReadLine());

               }

             

               //Check if guess is random

               if (guess == random)

               {

                   //print message

                   Console.WriteLine("Congratualations!");

                   Console.WriteLine("Correct! You got it in " + tries + " guesses!");

                   //reset tries to zero

                   tries = 0;

                   Console.WriteLine("Enter a guess between 1 and 10");

                   //read guess from user

                   guess = Convert.ToInt32(Console.ReadLine());

                   // random number between 1 and 10

                   random = rand.Next(10);

               }

                                               

           }//end of while

           quit = Convert.ToInt32(Console.ReadLine());

           if(quit==0)

           end;

       }//end of main

   }

}