Results 1 to 7 of 7
Thread: on_off
- 10-03-2012, 01:58 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
- 10-03-2012, 02:38 PM #2
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.htmlIf you don't understand my response, don't ignore it, ask a question.
- 10-03-2012, 02:44 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
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;
}
}
}
}
- 10-03-2012, 03:13 PM #4
Re: on_off
Please edit your post and wrap the code in code tags. See: BB Code List - Java Programming Forum
Yes, until on_off is not 1while (on_off==1) simply runs the while loop until it is falseIf you don't understand my response, don't ignore it, ask a question.
- 10-03-2012, 05:39 PM #5
Re: on_off
Why use a numeric (int) variable to test a boolean condition? Or haven't you yet been taught booleans?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-03-2012, 06:22 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
Re: on_off
what would you suggest?
- 10-03-2012, 07:30 PM #7
Re: on_off
A boolean variable.
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
dbWhy do they call it rush hour when nothing moves? - Robin Williams


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks