Java Bootcamp – Workbooks and Challenges
)}

Workbook 5.11

From the Java Bootcamp Resources, launch Workbook 5.11.

11.png

Task 1

Print the rules to the user:

>> Let's play Rolling Java. Type anything to start.

scan.nextLine

>> 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:

scan.nextLine

Task 2

Make a while loop that runs forever.

Place your second scan.nextLine() from Task 1 as the first line in your loop. Print hey in your loop's second line.

After testing your code, remove the hey print.

Task 3

Define the rollDice function.

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

Task 4

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

Task 5

Whenever the user rolls a 6:

  • print: End of game.
  • stop the game.

Whenever the user rolls a 4:

  • print: Zero points. Keep rolling.

When the user rolls anything else:

  • update the score variable by 1

  • print: One point. Keep rolling.

Task 6

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:

Visualizing the Runtime

After you solve this workbook, I still recommend watching the video solution on Udemy.

11.png

It will show you how to visualize the runtime using Visual Studio Code.


Associated Course: The Complete Java Development Bootcamp
Related Course: The Complete Spring Boot Development Bootcamp – Become a Java Web Developer

Feedback Summary
4.7
43 students
5

91%
4

2%
3

0%
2

0%
1

7%
Written Reviews
There are no written reviews yet.