Home > Blockchain >  How in C# with the "for" operator can you make the correct loop for the program?
How in C# with the "for" operator can you make the correct loop for the program?

Time:02-14

Guess the number game.

I am still a beginner and I need to use only those operators that are in the code, I am forbidden to use something else. The program gives the user 3 attempts to guess the number, but if the user goes out of range and writes for example -1 or 11, then the program should give the word "error" and should not consider this as an attempt.

I understand that you need to use a "for" loop,

    int a;
    int b = 0;
    for (a = 1; a < 11; a  )
    {
        b = b   a;
    }
    Console.WriteLine(b);

but I can not implement my idea. I will be grateful if you help!

         int a;
         int aa;
         a = 5;
         Console.WriteLine("Guess my number from 1 to 10");
         aa = Convert.ToInt32(Console.ReadLine());

         int b;

         for (b = 0; b < 2; b  )
         {
             if (aa == a)
             {
                 Console.WriteLine("Guessed");
             }
             else if (aa < a)
             {
                 Console.WriteLine("more");
                 aa = Convert.ToInt32(Console.ReadLine());
             }


             else
             {
                if (aa > 10)
                {

                    for ()
                    {
                     
                        Console.WriteLine("error");
                        aa = Convert.ToInt32(Console.ReadLine());
                    }

                }
                else if (aa < 1)
                 {
                     Console.WriteLine("error");
                     aa = Convert.ToInt32(Console.ReadLine());
                 }
                 else
                 {
                     Console.WriteLine("less");
                     aa = Convert.ToInt32(Console.ReadLine());
                 }
             }
         }

CodePudding user response:

Written on mobile, but this should work.

  int a;
     int aa;
     a = 5;

     for (int b = 0; b < 2; b  )
     {

         Console.WriteLine("Guess my number from 1 to 10");
          aa = Convert.ToInt32(Console.ReadLine());

         if(aa>10 ||aa<=0){
            Console.WriteLine("error");
          b--;
             continue;
        } 
         if (aa == a)
         {
             Console.WriteLine("Guessed");
              break;
         }
         else if (aa < a)
         {
             Console.WriteLine("more");
         }
            else {
                 Console.WriteLine("less");
             }
         }
     }

CodePudding user response:

I have made a game like that for you.

        const int number = 5;
        const int attempts = 3;

        for (var i = 1; i <= attempts; i  )
        {
            Console.WriteLine($"Attempt number: {i}/{attempts}");

            Console.WriteLine("Guess my number from 1 to 10");

            if (!int.TryParse(Console.ReadLine(), out var guessedNumber))
            {
                Console.WriteLine("Wrong Format!");
                i--;
                continue;
            }

            if (number == guessedNumber)
            {
                Console.WriteLine("Guessed");
                break;
            }

            if (guessedNumber > 10 || guessedNumber < 1)
            {
                Console.WriteLine("Error!");
                i--;
            }

            if (i != attempts)
            {
                Console.WriteLine("Try again");
                Console.WriteLine("*****************************");
            }
            else
                Console.WriteLine("Failed");
        }
        Console.ReadLine(); //use for program wait there.

CodePudding user response:

I think your main problem is getting the user input. You should take it to its own method:

private static int GetUserInput()
{
    while(true) // could also be `for(;;)` if you are not allowed to use `while`
    {
        Console.WriteLine("Enter a number from 0 to 10:");
        if(int.TryParse(Console.ReadLine(), out int userInput))
        {
           if( userInput >= 0 && userInput <= 10 )
           {
                return userInput;
           }
        }
        
        Console.WriteLine("error.");
    }
}

Now you can be sure to get a valid guess and concentrate on the main game flow.

CodePudding user response:

There's no need for multiple for loops, you just need one, the main "attempt loop". Let's break it down to the fundamental steps of the program:

  1. Get user input
  2. "Bounds check" the input
  3. Check if the user guessed correctly or not
  4. Repeat if incorrect guess

And we want to repeat a maximum of 3 times.

Using that we can start to program:

Step 1: Taking user input

Console.WriteLine("Guess the number (0-10):")
string guessInput = Console.ReadLine();
int guess = Convert.ToInt(guessInput);

Step 2: Bounds checking:

// Read: "If guess is smaller than 0, or guess is greater than 10
if (guess < 0 || guess > 10)
{
    Console.WriteLine("error");
    guesses--;
}

Step 3: Checking if the guess is correct or not:

if (guess == target)
{
    Console.WriteLine("success");
}

Step 4: Repeat

int target = 5;
for (int guesses = 0; guesses < 3; i  )
{
    // Code from steps 1 to 3 goes here
}

Putting it all together:

(Spoiler warning! If you want to solve it yourself using the knowledge above then stop reading

int target = 5;
for (int guesses = 0; guesses < 3; i  )
{
    Console.WriteLine("Guess the number (0-10):");
    string guessInput = Console.ReadLine();
    int guess = Convert.ToInt(guessInput);

    if (guess < 0 || guess > 10)
    {
        Console.WriteLine("error");
        // Invalid input, so we're subtracting 1 from the total guesses
        guesses--;
    }

    if (guess == target)
    {
        Console.WriteLine("Guessed");
        // Congrats, now we want to stop the loop.
        // We do this by invalidating the condition, i.e making 'guesses < 3' false
        guesses = 10; // Any number larger than 3
    }

    if (guess > target)
    {
        Console.WriteLine("more");
    }
    else if (guess < target)
    {
        Console.WriteLine("less");
    }
}
  • Related