Results 1 to 10 of 10
Thread: Trouble with While Statement
- 07-15-2012, 11:17 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
Trouble with While Statement
I need to make a section of code re- run depending on user input. I tried using a while loop but I could not get it working!
Java Code:import java.util.Scanner; import java.util.Random; public class Game { public static void main(String args[]){ System.out.println("Welcome to the Gumball Guessing Game"); System.out.println("Please enter your first Guess!"); Scanner input = new Scanner(System.in); Random gumballs = new Random(); int number; int gum; for(int counter=1; counter<=1;counter++){ gum = gumballs.nextInt(5); number = input.nextInt(); if(number == gum){ System.out.println("CORRECT!!! You won!"); } } }
Last edited by JosAH; 07-15-2012 at 11:31 AM. Reason: added [code] ... [/code] tags
- 07-15-2012, 11:34 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Trouble with While Statement
Your for loop only runs its body once (counter > 1 on the second pass); is that what your problem is about?
kind regards,
Jos
ps. fix your indentation.Build a wall around Donald Trump; I'll pay for it.
- 07-15-2012, 06:15 PM #3
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 8
Re: Trouble with While Statement
Yeah, the problem here seems to be that there isn't a while statement.
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 07-16-2012, 01:40 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
Re: Trouble with While Statement
Hi,
How about trying to make a while statement then?
Java Code:while(number != gum)
- 07-16-2012, 12:39 PM #5
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
Re: Trouble with While Statement
well I tryed to fix it with a while loop again but to no avail. Sorry if I seem really dumb... just new to Java!
Java code:
import java.util.Scanner;
import java.util.Random;
public class Game {
public static void main(String args[]){
System.out.println("Welcome to the Gumball Guessing Game");
System.out.println("Please enter your first Guess!");
Scanner input = new Scanner(System.in);
Random gumballs = new Random();
int number;
int gum;
while(number <||> gum){
for(int counter=2; counter<=5;counter++){
gum = gumballs.nextInt(5);
number = input.nextInt();
}
}
if(number == gum){
System.out.println("CORRECT!!! You won!");
}else{
System.out.println("WRONG");
}
}
}
- 07-16-2012, 01:27 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Trouble with While Statement
What is that '<||>' thing?
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 07-17-2012, 12:31 AM #7
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
Re: Trouble with While Statement
greater than or less than.
- 07-17-2012, 01:31 AM #8
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 8
Re: Trouble with While Statement
<||> is not a valid boolean operator in Java, I'm afraid. The most convenient way to do an equivalent test that I know of is:
Java Code:(number < gum || number > gum)
Java Code:(number != gum)
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 07-20-2012, 07:47 AM #9
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
Re: Trouble with While Statement
I did it like you said and it now says that the variables gum and number may not have been initialized?
- 07-20-2012, 08:03 AM #10
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
the switch statement and unreachable statement error
By name in forum New To JavaReplies: 2Last Post: 03-26-2012, 05:27 PM -
Trouble with recursion if statement.
By david522 in forum New To JavaReplies: 3Last Post: 09-28-2011, 03:41 PM -
new to java, trouble with if statement
By Globular in forum New To JavaReplies: 8Last Post: 05-25-2011, 07:03 PM -
having some trouble
By Unknown1369 in forum New To JavaReplies: 13Last Post: 07-22-2008, 12:52 AM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 05:45 PM
Bookmarks