Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Random Boolean in an if statement (tricky, can't seem to figure out)

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Austin
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Random Boolean in an if statement (tricky, can't seem to figure out)

    Okay, so I have a class and in this class I have a method that runs a random boolean generator and I want to make it so that, depending on the value, it does one thing or the other. It's part of the battle class of my console game, Dungeoneer.(Which I will make a thread for soon, I may need help there lol).

    Anyhow, here is the current code, and it has a massive error that I can't seem to work around. Basically, I want
    to make the method blockCheck be a part of the battle system and essentially it works like this: "if player block true then take no damage, else take random damage(which I'll make shortly).

    import java.util.Random;
     
    //This class is the battle system
     
    public class Battle {
     
        private static Random rnd = new Random();
     
        public static boolean blockCheck() {
            return rnd.nextBoolean();
            if (blockCheck() == false) {
                //TODO block code
            }
            else {
               //TODO damage code
     
           }
       }
     
    }

    So, what can I do to make this if statement work without error(aside from actually putting code in it)?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Random Boolean in an if statement (tricky, can't seem to figure out)

    Quote Originally Posted by Xhalite View Post
    So, what can I do to make this if statement work without error(aside from actually putting code in it)?
    What did that massive error say?
    Always post the full text of your error message with the code and question.
    What does this line do?
    return rnd.nextBoolean();

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    Austin
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Boolean in an if statement (tricky, can't seem to figure out)

    The error on the if statement is "unreachable statement". The error on "public static boolean..." is "missing return statement, missing javadoc". I thought I did put in a return statement and I know the javadoc is known to my IDE(netbeans). What is wrong with this code?

    also that line returns a random boolean value, so it'll randomly say true or false. But I need it to be put into an if statement somehow.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Random Boolean in an if statement (tricky, can't seem to figure out)

    Unreachable statement.
    Now think about the line of code we discussed, the one that returns a value. How can that if else ever be reached if you return a boolean one line earlier?


    Think about the flow of the if statement:

    if( myRandomBooleanGenerator.returnsTrue )
    --then thisMethodReturnsTrue
    --else thisMethodReturnsFalse

    Can you see how the if/else is not even necessary?

  5. #5
    Junior Member
    Join Date
    May 2013
    Location
    Austin
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Boolean in an if statement (tricky, can't seem to figure out)

    Quote Originally Posted by jps View Post
    Unreachable statement.
    Now think about the line of code we discussed, the one that returns a value. How can that if else ever be reached if you return a boolean one line earlier?


    Think about the flow of the if statement:

    if( myRandomBooleanGenerator.returnsTrue )
    --then thisMethodReturnsTrue
    --else thisMethodReturnsFalse

    Can you see how the if/else is not even necessary?
    But the if statement is necessary, because I then use the blockCheck method to do exactly what it says, check if the player blocks or not based on the true/false(block/!block) in the Game class (which I might have to post). Maybe there's a better way? Perhaps I could use Math.random... Basically I'm calling blockCheck(); in a different class at certain points in the game.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Random Boolean in an if statement (tricky, can't seem to figure out)

    Ok I see what you are doing with the if now.
    The same thing applies to the return statement. When the method returns, that is the exit door. All code must be before the return statement

  7. #7
    Junior Member
    Join Date
    May 2013
    Location
    Austin
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Boolean in an if statement (tricky, can't seem to figure out)

    Thanks, I figured it out! I put the if statement before the return value and it worked like a charm!

Similar Threads

  1. IF statement accumulataion not working properly - random numbers
    By StrugglerWithJava in forum Loops & Control Statements
    Replies: 1
    Last Post: April 4th, 2013, 08:48 AM
  2. How to use the Random statement to generate a random attack.
    By Shzylo in forum Java Theory & Questions
    Replies: 5
    Last Post: March 3rd, 2013, 07:05 PM
  3. Game of Life 2-D matrix random seed boolean array PLEASE
    By Eriosblood in forum Collections and Generics
    Replies: 20
    Last Post: September 3rd, 2012, 07:10 PM
  4. [SOLVED] (Simple) How to use an if statement to respond to random integers
    By DusteroftheCentury in forum Loops & Control Statements
    Replies: 9
    Last Post: January 27th, 2012, 09:15 PM

Tags for this Thread