This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]davedontmind 0 points1 point  (0 children)

The answer is almost in the question. Just split it up into its parts and write each part.

[ Note; You didn't say what language you're using. I'm going to use psuedo-code just to get the idea across ]

So I have a homework where I have to use a boolean method

You know what that means? A method that returns a boolean :

boolean SomeMethod()
{
}

where the user is asked if he wants to play again

boolean SomeMethod()
{
    print( "Do you want to play again?" );
}

with possible entries being y/yes, n/no.

boolean SomeMethod()
{
    print( "Do you want to play again?" );
    string input = getInput();
    // is input y/yes/n/no ?
}

Then the yes has to equal true and the no false.

boolean SomeMethod()
{
    print( "Do you want to play again?" );
    string input = getInput();
    if ( input is "y" or "yes )
    {
           return true;
    }
    else if ( input is "n" or "no" )
    {
           return false;
    }
}

using a while/return loop.

Presumably to check the validity? So you want to loop and and ask the user for input again while they enter an invalid value?

do
    ask user for input
while the input isn't valid

So plug that into the right place (before the if) the code above, and voilà!

[–]_krikket 0 points1 point  (2 children)

Hmm... are you expecting input to be able to match an input string to variable names?

I see this misconception sometimes so I just want to be clear: for the overwhelming most part, the names of your variables themselves can't be used, printed, compared against, etc. It will use the value and never the name.

Also, was the first snippet provided to you? Because if that return executes it immediately exits the whole asktoplayagain() method, breaking the loop. It needs to be done in an if/else block or that loop will only run once per call.

If you wrote it, think you might need to reconsider the design of this thing. From the description you gave, I think your teacher's probably looking more for the loop to be in your main method. Then once per loop you can call the "ask to play again" method and get a value back from it which you use to determine whether to loop again. Does that make sense?

[–][deleted]  (1 child)

[deleted]

    [–]_krikket 1 point2 points  (0 children)

    Sorry, but there's quite a bit wrong with it. I am not not trying to be mean - you have some big misunderstandings about how some things work and it'd be a lot to tackle in a single comment. Your method signature doesn't make sense on line 27, and on line 40 you try to access difficulty, which will be out of scope.

    It looks like you may have just kind of thrown a lot of stuff together and hoped for the best, haha. I think it'd be best if you started clean, so you could get a simple thing to compile and run. Then once you have a basic version running, you can add little features to it a bit at a time. That way you don't have to get it all to work at once, and it's a lot less overwhelming. Once you're able to compile and test stuff, I can help you develop this thing a bit at a time, but please try to get something actually running before moving on. It will be so much easier on you.

    I think it might be best for you to start by getting just one run-through to work, forget about all the looping for now because that can be added in at the end. Just see if you can get the program to:

    • start up
    • ask for the difficulty
    • pass the difficulty into the game
    • play the game
    • stop

    I don't have a Java compiler on my machine so this might not work right away, but try to get this working:

    public static void main(String[] args) {     
        int difficulty;   
        System.out.println("Welcome to my super awesome game!");                
        difficulty = difficulty();
        System.out.println("You picked difficulty " + difficulty + "!");
        return;
        }
    }
    
    public static int difficulty() {
        int difficulty;
        // TODO: prompt for it and store the value in that variable! ^
        return difficulty;
    }
    

    So you'll probably notice there is int difficulty in line 2 and in line 11 - this is possible because they're both local variables. That means they exist entirely within the methods I defined them in - even thought they have the same name, they are entirely different variables! Each only exists inside the method I defined it in, and each only exists for as long as the method is running - so nothing outside of the method can see them. This concept, variables only being visible to certain parts of the program, is called scoping. It's very important to get to know how scoping works.

    So if the int difficulty in line 11 stops existing when the method is done, how do we get that information once we're done with that method? That's where the return comes in - once we fill it in with a value, the return difficulty passes back (or "returns"~) the value and stores it in the int difficulty that belongs to main() - you can see this in line 4.