0
votes

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!");
                }   
            }
        }
    }
}
2
Consider using a while loopauburg
new Random().Next(1, 100) is poor for two reasons. (1) it's bad practice to new` up multiple instances as it can lead to duplicate values. (2) If you want numbers from 1 to 100 you need to put .Next(1, 101).Enigmativity

2 Answers

0
votes

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********");
    }
}
0
votes

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}.");
    }