Java Bootcamp Resources ->
Module 1 ->
5. Loops, open Workbook 5.11.
Rolling Java is a game of luck. If the user rolls:
6: the game stops.
4: nothing happens.
anything else: 1 point.
The user wins if they collect at least 3 points before the game ends.
- First, print:
"Let's play Rolling Java. Type anything to start.\n"
- Print the rules:
"Great, here are the rules:\n"
>> - If you roll a 6 the game stops.
>> - If you roll a 4 nothing happens.
>> - Otherwise, you get 1 point\n.
>>You must collect at least 3 points to win. Enter anything to roll:
The last line implies the usage of
Scanner because it needs to give the user time to read the rules.
while loop that runs forever.
Place your last
scan.nextLine() as the first line in the
while loop. In the second line, print "hey" for now:
Remove the "hey" print.
/** * Function name: rollDice * @return randomNumber (int) * * Inside the function: * - return a random number between one and six. */
During each run, call
rollDice() and store the value in
diceRoll. Print each dice roll:
You rolled a <diceRoll>..
During each run:
If the user rolls a 6:
End of game.
stop the game.
If the user rolls a 4:
Zero points. Keep rolling.
If the user rolls anything else:
pointsvariable by 1
One point. Keep rolling.
println skills by trying to achieve a similar output:
After the game ends, check the user's points.
If the score is greater than or equal to 3, print:
Wow, that's lucky. You win!
Tough luck, you lose :(