Java Bootcamp – Workbooks and Challenges

Workbook 5.11

From Java Bootcamp Resources -> Module 1 -> 5. Loops, open Workbook 5.11.

Rolling Java

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.

Task 1

  • 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.

Task 3

Make a 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.

Task 4

Define the rollDice function.

/** * Function name: rollDice * @return randomNumber (int) * * Inside the function: * - return a random number between one and six. */

Task 5

During each run, call rollDice() and store the value in diceRoll. Print each dice roll: You rolled a <diceRoll>..

Task 6

During each run:

  • If the user rolls a 6:

    • print: End of game.

    • stop the game.

  • If the user rolls a 4:

    • print: Zero points. Keep rolling.
  • If the user rolls anything else:

    • update the points variable by 1

    • print: One point. Keep rolling.

Test your print and println skills by trying to achieve a similar output:

Task 7

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!
  • Otherwise, print:

    • Tough luck, you lose :(

Winning scenario:

Losing scenario:

Good Luck!

Feedback Summary
39 students





Written Reviews
There are no written reviews yet.