Question Anyone that can please help with this?

Kaydoe

Member
Joined
Apr 3, 2021
Messages
5
Programming Experience
Beginner
C#:
uses System;

public class program
{
    public static void Main ()
    {
        Random randomerare = new Random ();
        was randomNumber = random number. Next (1,101);

        int-counter = 1;
        var maxGuesses = 8;

        for (int i = 0; i <8; i ++) / * First used the variable “i” as my counter
            but when I did, I got that after a guess that I had all the guesses left.
            Set "i" to 1 instead of 0 but then I came to my first if I set immediately and the program ended.
                What can I do if I want to use "i" correctly.
                    * / {
                    // while (counter! = maxGuesses)

                    Console.WriteLine ("Guess a number between 1-100:");
                    var userGuess = Convert.ToInt32 (Console.ReadLine ());

                    if (counter == maxGuesses)
                    {
                        Console.WriteLine ("Game over! You've finished your 8 guesses!");
                        break;
                    }
                    om (userGuess> randomNumber)
                    {
                        / * if (randomNumber> 70 && userGuess> 70)
                        {

                        } * /
                            Console.WriteLine (userGuess + "Is too high, try again ..." + "\ nYou have" + (maxGuesses counter) + "guesses left ...");
                        counter ++;
                    }
                    otherwise if (randomNumber> userGuess)
                    {
                        Console.WriteLine (userGuess + "Is too low, try again ..." + "\ nYou have" + (maxGuesses counter) + "guesses left ...");
                        counter ++;
                    }
                    Other
                    {
                        Console.WriteLine (userGuess + "Is the correct number!" + "\ NYYou guessed {0}" + counter + "times!");
                        break;
                    }

                }
    }
}
 
Last edited by a moderator:
This is a very poorly constructed question. Please start by providing a FULL and CLEAR explanation of the problem. That means a description of what you're trying to achieve, how you're trying to achieve it and what happens when you try. The explanation of the problem does not belong in the code. Finally, provide a title that summaries the issue. Your title is as much use as no title at all.
 
This is a very poorly constructed question. Please start by providing a FULL and CLEAR explanation of the problem. That means a description of what you're trying to achieve, how you're trying to achieve it and what happens when you try. The explanation of the problem does not belong in the code. Finally, provide a title that summaries the issue. Your title is as much use as no title at all.

sorry! I am trying to make a guessing game and I tried to implement "try and catch" on the console.write line and read line but on the remaining code a lot of variables stop working. Can someone please help me?
 
Use Int.TryParse() instead of using exception handling as flow control.
 
Your for loop already limits the number of guesses. You don't need your counter. Instead of checking inside the loop if the user has run out of guesses, just tell the user how many guesses they have left.

Using try-catch to determine if the user entered a valid integer is the naive way of doing input validation. Using exceptions to do flow control should be your last choice because it's almost as evil as using goto's but more subtle. Using TryParse() to determine if the user entered an integer or not.
 
So what’s the smart way of doing this? Sorry for my ignorance it has been 9 years since I sat down with csharp
 
You are already 80% of the way there. If you compare the pseudo code I will suggest below, you will see that you have most of the elements you need for success:

C#:
int randomNumber = get random number
int value = -1;

for(int i = 0; i < maxGuesses; i++)
{
    int guessesLeft = maxGuesses - i;
    Console.WriteLine("You have {0} of {1} guesses left. Enter your guess:", guessesLeft, maxGuesses);

    string input = Console.ReadLine()
    if (try parse input into value fails)
    {
        print "You just wasted a guess by entering a non-integer value";
        continue back to the top of the for loop
    }
    else if (value == randomNumber)
    {
        print "You guessed right!"
        break out of the for loop
    }
    else if (value < randomNumber)
    {
        print "Too low."
    }
    else
    {
        print "Too high."
    }
}
 

Latest posts

Back
Top Bottom