Please help with the swtich case need for a game
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please Enter a number");
int day = input.nextInt();
switch(day)
{
case 1: System.out.println("1 Microphone");
break;
case 2: System.out.println("2 Loud Speakers 1 Microphone ");
break;
case 3: System.out.println("3 Keyboards 2 Loudspeakers 1 Microphone ");
break;
case 4: System.out.println("4 Java Books 3 Keyboards 2 Loudspeakers 1 Microphone");
break;
case 5: System.out.println("5 Iphones 4 Java Books 3 Keyboards 2 Loudspeakers 1 Microphone");
break;
default: System.out.println("Enter A Valid Prize Day");
}
}
As @AlexandreSantos pointed out, you need to reinitialise the values of
maxRolls
andsum
every time you restart the game. That is, these initialisations should be the first things executed in yourdo {} while ()
loop.I'd also give you other recommendations:
Game
instead ofgame
.The following code (and its equivalent with
"no"
):... can be replaced by:
... since, as you mentioned in your question, you're actually simply trying to ignore the case ;)
"You won"
or"You lost"
.I'd suggest to replace:
... by:
... or, even better, extracting this into an other method.
Or, even better, not even checking for one of the possibilities and make it the default one, in case the user enters something that's neither
"yes"
nor"no"
:... which can obviously then become:
... and the
option
variable could disappear:Random
instead ofMath.random()
, it's just way more convenient.For example:
... could become:
Scanner
before leaving the program.Use the try-with-resources syntax:
sum % 10 == 0
is weird: you've already told the user that he won if he scored at least 43, and he's gonna lose if he scored less than 43... You should either:Test that condition before checking whether the user has scored more than 43 (and therefore also rejecting scores like 50, 60, 70, 80...)
... or:
Forget about that rule that only aims to reject 10, 20, 30 and 40, which are already covered by the
score < 43
rule.Cheers ;)
Just 'cause I felt bored, I actually applied my own advices (and a few more) to your code:
Sounds like you want an outer loop; each time through the loop the user plays one game. At the top of that loop, you initialize the values that you need to play one game:
I have suggested a change to a while loop that executes as long as the
maxRolls
has not been reached. It is not a good idea to modify the target of a for loop within the loop; in some languages, at least, the behavior is undefined, and it confuses the reader. SincemaxRolls
can change, you need a different looping form there.And you don't really need to call
System.exit()
; if you "fall out of" the bottom of your main routine, your program will just exit since it has no more instructions to execute.I don't recommend
do while(true)
in this case; the (small) problem with it is that it makes it harder for the reader to determine when the loop exits. Not a big deal.Good luck.