Diasalva
Diasalva

Reputation: 3

console application where the user has 5 tries to guess number between 1 and 100

I have created a console application where the user has 5 tries to guess number between 1 and 100. After 5 guesses the game ends, but I don’t know how to introduce at the 5th wrong intent something like “you have achieved maximum of guesses! The answer was number (X). I have tried different ways ,but is not working. This is my program

using System;

namespace Guessing_Game_4
{
    class Program
    {
        static void Main(string[] args)
        {
            var number = new Random().Next(1, 100);
            Console.WriteLine("Try and guess any number between 1-100. You have 5 guesses Max!");

            for (var i = 0; i < 5; i++)
            {
                int guess = Convert.ToInt32(Console.ReadLine());               
                if (guess == number)
                {
                    Console.WriteLine("You got it!");
                    break;
                }
                else
                {
                    Console.WriteLine(guess + " is not correct! Try again!");
                }   
            }
        }
    }
}

Upvotes: 0

Views: 849

Answers (4)

Ghasaq Ali
Ghasaq Ali

Reputation: 1

class Program

{
    static void Main(string[] args)
    {
             var number = new Random().Next(1, 100);
             Console.WriteLine("Try and guess any number between 1-100. You have 5 guesses Max!");

            for (var i = 0; i < 5; i++)   
          {
              int guess = Convert.ToInt32(Console.ReadLine());               
             if (guess == number && i!=5)
            {
                Console.WriteLine("You got it!");
                break;
            }
            else
            {
                Console.WriteLine(guess + " is not correct! Try again!");
            }  
            
        }
            Console.WriteLine(" the maximam guse");        
        
    }

} //Try this one

Upvotes: 0

Sajed
Sajed

Reputation: 1976

  1. You have to have an int outside of your loop like this : int wrongAnswersCount = 0;
  2. When the user enter a wrong number you should add one unit to your variable wrongAnswersCount++;
  3. In the start of the loop, you should check if the user reached the maximum amount of gueses or not, if yes break the loop and say the answer.

Your code will be something like this :

using System;

namespace Guessing_Game_4
{
    class Program
    {
        static void Main(string[] args)
        {
            var number = new Random().Next(1, 100);
            Console.WriteLine("Try and guess any number between 1-100. You have 5 guesses Max!");
            int wrongAnswersCount = 0;
            for (var i = 0; i < 5; i++)
            {
                if(wrongAnswersCount == 5)
                {
                    Console.WriteLine($"you have achieved maximum of guesses! The answer was number {number}");

                    break;
                }
                int guess = Convert.ToInt32(Console.ReadLine());
                if (guess == number)
                {
                    Console.WriteLine("You got it!");
                    break;
                }
                else
                {
                    Console.WriteLine(guess + " is not correct! Try again!");
                    wrongAnswersCount++;
                }
            }
        }
    }
}

Upvotes: 0

Youssef13
Youssef13

Reputation: 4986

If that's all your program does, you can do the following trick. Print your message after the for loop, but now the problem is that you get the message in all cases. The trick is to return from the Main (instead of breaking the loop) on a correct guess:

Console.WriteLine("You got it!");
return;

If you've some other code to execute that returning from Main won't be a good solution, you can do the following:

  1. Create a variable before the for loop. Let's call it isCorrectAnswer and set it to false in the beginning.

  2. At the point where he answers correctly, set isCorrectAnswer to true before breaking the loop.

  3. After the loop, check for that variable:

    if (!isCorrectAnswer)
    {
        Console.WriteLine($"you have achieved maximum of guesses! The answer was number {number}.");
    }
    

Upvotes: 0

ch Noman
ch Noman

Reputation: 19

Here some Sample code This might help

        for( i=10;i>0 ; i--) {
        System.out.println("    === you have " + i +" Guesses left ===");

        int guess = scanner.nextInt();
        if (random_number < guess) System.out.println("Smaller than guess  " + guess);
        if (random_number > guess) System.out.println("Greater  than guess " + guess);
        if (random_number == guess)
        {
            result = true;
            break;
        }
    }
    if (result)
    {
        System.out.println("You WON in "+(10-i) +" tries ");
        System.out.println("******* CONGRATULATIONS **************************************");
        System.out.println("*********************** YOU WON **********************");
    }
    else
    {
        System.out.println("The random number was   "+random_number);
        System.out.println("**************************  OPPS You loose **************************************** ");
        System.out.println("You are near it TRY Next time ************ GOOD LUCK ");
        System.out.println("You are near it TRY Nexttime***********NEXTTIME********");
    }
}

Upvotes: 0

Related Questions