Results 1 to 10 of 10
- 02-05-2012, 09:23 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 26
- Rep Power
- 0
How do I make this case execute correctly in switch statement?
I have the switch statement working nicely im just having a hard time figuring out how to get this code to do what I want. When I run it now it just prints one time. How would I get this to execute until I get a winning match and how would I create a counter to know how many times it took?
case 2: menu = "Spin wheels until a winning match. Find out how many times it took to win";
do{
one = gen.nextInt(5) + 1;
two = gen.nextInt(5) + 1;
three = gen.nextInt(5) + 1;
} while (one != two && two != three);
System.out.println(+ one + " " + two + " " + three);
break;
- 02-05-2012, 11:01 PM #2
Re: How do I make this case execute correctly in switch statement?
how would I create a counter to know how many times it took?
When I run it now it just prints one time.
Why do you expect it to print more than once?
- 02-05-2012, 11:14 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 26
- Rep Power
- 0
Re: How do I make this case execute correctly in switch statement?
I know theres not. Im supposed to create a slot machine. I need that loop to repeat itself and print all the numbers until they all equal each other.
- 02-05-2012, 11:26 PM #4
Re: How do I make this case execute correctly in switch statement?
If you want to print all the numbers, move the println statement inside of the loop.
- 02-05-2012, 11:38 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 26
- Rep Power
- 0
Re: How do I make this case execute correctly in switch statement?
ok i did that but its still not executing like i want it to. I want it to continue to print the numbers until they all match up
- 02-05-2012, 11:46 PM #6
Re: How do I make this case execute correctly in switch statement?
The println inside of the loop will print one time for every time around the loop.
Can you post the output from your program where it does not continue printing every time around?
How do you want it to print?
- 02-05-2012, 11:50 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 26
- Rep Power
- 0
Re: How do I make this case execute correctly in switch statement?
this is my code:
case 2: menu = "Spin wheels until a winning match. Find out how many times it took to win";
do{
one = gen.nextInt(5) + 1;
two = gen.nextInt(5) + 1;
three = gen.nextInt(5) + 1;
System.out.println(+ one + " " + two + " " + three);
} while (one != two && two != three);
break;
I want it to print out until all the integers match up. IE
013
253
124
254
555
- 02-05-2012, 11:51 PM #8
Re: How do I make this case execute correctly in switch statement?
What does your program print out now?
Since it is using random numbers, every execution will generate a different group of numbers.
How are you defining the gen variable?
- 02-05-2012, 11:57 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 26
- Rep Power
- 0
Re: How do I make this case execute correctly in switch statement?
heres the whole statement with the gen variables:
String menu = " ";
switch (option){
case 1: menu = "Pull handle once and print results";
one = gen.nextInt(5) + 1;
two = gen.nextInt(5) + 1;
three = gen.nextInt(5) + 1;
System.out.println(+ one + " " + two + " " + three);
if (one == two && two == three) {
System.out.println("Winner!");
}
break;
case 2: menu = "Spin wheels until a winning match. Find out how many times it took to win";
do{
one = gen.nextInt(5) + 1;
two = gen.nextInt(5) + 1;
three = gen.nextInt(5) + 1;
System.out.println(+ one + " " + two + " " + three);
} while (one != two && two != three);
break;
case 3: menu = "Quitter! Goodbye!";
break;
}
this is what it prints out currently after selecting option 2.
3 1 3
2 2 3
Please select an option (1 or 2 or 3):
1: Pull handle once and print results
2: Spin wheels until a winning match. Find out how many times it took to win
3: Quit
It prints something different everytime but it doesnt continue to print till all the numbers match
- 02-06-2012, 12:02 AM #10
Re: How do I make this case execute correctly in switch statement?
but it doesnt continue to print till all the numbers match
You need a new condition to test to keep the loop going.
Similar Threads
-
Using a switch/case with my if statements
By coding in forum New To JavaReplies: 2Last Post: 03-07-2011, 09:01 AM -
Switch Case statement
By seanfmglobal in forum New To JavaReplies: 7Last Post: 02-15-2011, 02:18 PM -
if else changes to switch-case?
By noobinoo in forum New To JavaReplies: 1Last Post: 04-23-2010, 06:56 PM -
[SOLVED] Is it possible to have If in a switch case?
By sfe23 in forum New To JavaReplies: 2Last Post: 02-23-2009, 01:34 AM -
Switch Case and Key Events
By AndrewM16921 in forum New To JavaReplies: 4Last Post: 01-27-2009, 12:20 AM
Bookmarks