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;
Re: How do I make this case execute correctly in switch statement?
Quote:
how would I create a counter to know how many times it took?
Define a int variable outside the loop, and increment it inside the loop.
Quote:
When I run it now it just prints one time.
There is nothing in the code that would make it print more than one time per execution of the switch statement with a value of 2.
Why do you expect it to print more than once?
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.
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.
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
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?
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
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?
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
Re: How do I make this case execute correctly in switch statement?
Quote:
but it doesnt continue to print till all the numbers match
Look at the condition that controls the loop. With an AND relationship both of the conditions must be true for the whole expression to be true. If one is true and the other false, the the whole expression is false.
You need a new condition to test to keep the loop going.