Results 1 to 7 of 7

Thread: on_off

  1. #1
    helpplease is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default on_off

    Can someone please explain what: while(on_off==1) does

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: on_off

    It compares (==) the value of the variable to the value 1. If true the statement following the while is executed.

    See: http://docs.oracle.com/javase/tutori...lts/while.html
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    helpplease is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: on_off

    Below is my code for a random number game.
    Can I assume that while (on_off==1) simply runs the while loop until it is false-meaning the number is correctly guessed? At which point on_off=0 thus exiting the loop?


    Random generator = new Random();

    int number = generator.nextInt(1024) + 1;
    int on_off = 1;
    int guess;
    String inputString;

    while(on_off == 1){
    inputString = JOptionPane.showInputDialog("Guess a number between 1 and 1024: ");
    guess = Integer.parseInt(inputString);
    if(guess < number){
    JOptionPane.showMessageDialog(null,"Too Low");
    }
    if(guess > number){
    JOptionPane.showMessageDialog(null,"Too High");
    }
    if(guess == number){
    JOptionPane.showMessageDialog(null,"Correct! You win!");
    on_off = 0;
    }
    }
    }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: on_off

    Please edit your post and wrap the code in code tags. See: BB Code List - Java Programming Forum

    while (on_off==1) simply runs the while loop until it is false
    Yes, until on_off is not 1
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: on_off

    Why use a numeric (int) variable to test a boolean condition? Or haven't you yet been taught booleans?

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

  6. #6
    helpplease is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: on_off

    what would you suggest?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

Posting Permissions

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