Results 1 to 7 of 7
  1. #1
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default How to use random booleans

    My question: how to trigger an event by using a random boolean?
    I want a action event to happen in my program but I only want the action performed if the boolean is true(for example by clicking on a button I want something to display or not if the boolean is true). I want to use a random boolean, and then use an if statement to test to see if the boolean is true and if so then display like a JDialog or something. Here is some of the code for what I am trying to do

    //rob boolean declared as true in constructor

    public boolean randomBoolean(){
    return Math.random() < 0.5;
    }

    private void LeaveTownActionPerformed(java.awt.event.ActionEven t evt) {
    rob=randomBoolean();

    if(rob==true){
    Dialog.showMessage("You have been robbed");
    }

    So why doesn't this code work?????????? Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: How to use random booleans

    When posting code, please use the code tags to preserve formatting, and please post in the form of an SSCCE so we can see exactly what you're doing and debug it ourselves.

    What exactly do you mean when you say this doesn't work? What happened when you stepped through it with a debugger or at least added some print statements to figure out what's going on?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: How to use random booleans

    Get in the habit of using standard Java naming conventions!

  4. #4
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to use random booleans

    Yeah so about the mess, you can tell I'm new to this stuff. The problem I am having is that the dialog won't appear in the program. I am doing this in the java swing editor. So far everything else in the program executes except for this bit of code.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How to use random booleans

    Quote Originally Posted by Snake006 View Post
    I am doing this in the java swing editor.
    The what? Java doesn't come with a Swing editor, nor any other type of editor.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: How to use random booleans

    What Java Swing editor? Netbeans?

    From the little code that you posted, I can only make wild guesses about what might be wrong. Can you write a SSCCE that demonstrates the problem?
    Get in the habit of using standard Java naming conventions!

  7. #7
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to use random booleans

    Oh its netbeans. I am using a JFrame form, which allows me to drag and drop buttons, etc. (This is a game I'm making) My code is several pages long, however everything else works fine. I will however include the essential functions with comments:

    private boolean rob;
    private boolean sword; //if this is false then the user might get robbed

    public TownRevisit() { //the constructor
    initComponents(); //all the graphics stuff
    this.setLocationRelativeTo(null);
    rob=true; //this is where I declare the boolean
    }

    private void LeaveTownActionPerformed(java.awt.event.ActionEven t evt){
    //this part has several different functions, basically when you click leave town a bunch of different things happen if you do something prior(for example buy an item which sets a boolean to true, which in true makes a specific JFrame appear)

    //I want the dialog you have been robbed to appear about half the time, if the user has left without buying a sword

    }

Similar Threads

  1. array of booleans to represent a set of integers
    By whateverme in forum New To Java
    Replies: 0
    Last Post: 05-16-2013, 09:22 PM
  2. Using Booleans for equality?
    By Maniacmike3128 in forum New To Java
    Replies: 1
    Last Post: 04-08-2011, 08:41 PM
  3. OOP newbie, booleans not working
    By gnng in forum New To Java
    Replies: 5
    Last Post: 04-01-2011, 09:54 PM
  4. Having problems with static booleans
    By Medieve in forum New To Java
    Replies: 4
    Last Post: 12-08-2010, 06:07 PM
  5. arrays with booleans
    By hoosierfan24 in forum New To Java
    Replies: 24
    Last Post: 10-04-2010, 01:07 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •