This question is from Programmingpraxis:

*Petals Around The Rose*. Here’s a sample game:

`Let's play 'Petals Around The Rose.The name of the game is significant. At each turn I will roll five dice, then ask you for the score, which will always be zero or an even number. After you guess the score, I will tell you if you are right, or tell you the correct score if you are wrong. The game ends when you prove that you know the secret by guessing the score correctly six times in a row.`

```
```The five dice are: 4 2 5 5 4.

What is the score? 6

The correct score is 8.

The five dice are: 2 5 1 3 3.

What is the score? 8

Correct

The five dice are: 2 5 2 5 6.

What is the score? 8

Correct

The five dice are: 6 2 5 3 2.

What is the score? 8

The correct score is 6.

The five dice are: 4 2 1 1 2.

What is the score? 6

The correct score is 0.

The five dice are: 5 6 3 6 6.

What is the score? 6

Correct

The five dice are: 2 6 2 2 4.

What is the score? 6

The correct score is 0.

The five dice are: 3 2 4 2 1.

What is the score? 0

The correct score is 2.

The five dice are: 4 4 2 6 3.

What is the score? 2

Correct

The five dice are: 2 1 5 1 5.

What is the score? 8

Correct

The five dice are: 1 5 5 2 4.

What is the score? 8

Correct

The five dice are: 5 2 4 6 3.

What is the score? 6

Correct

The five dice are: 1 6 1 5 3.

What is the score? 6

Correct

The five dice are: 1 4 3 2 6.

What is the score? 2

Correct

`Congratulations! You are now a member of the Fraternity of the Petals Around The Rose. You must pledge never to reveal the secret to anyone.`

Solution:

import java.util.*; public class Petals_Around_The_Rose { static String intro = "Let's play 'Petals Around The Rose." +"\nThe name of the game is significant." +"\nAt each turn I will roll five dice," +"\nthen ask you for the score, which" +"\nwill always be zero or an even number." +"\nAfter you guess the score, I will tell" +"\nyou if you are right, or tell you the" +"\ncorrect score if you are wrong. The game" +"\nends when you prove that you know the" +"\nsecret by guessing the score correctly"; public static void main(String[] args) { int count = 0;//counting number of correct answer System.out.println(intro); while(count < 5){ int [] a = roll(); System.out.println("\nThe five dice are: " + Arrays.toString(a)); System.out.print("What is the score?"); Scanner input = new Scanner(System.in); Integer ans = input.nextInt(); int expected = secret(a); if(ans == expected){ count++; System.out.println("Correct.\n"); } else { count = 0; System.out.println("The correct score is " + expected + "."); } } System.out.println("\nCongratulations! You are now a member" + "\nof the Fraternity of the Petals Around" + "\nThe Rose. You must pledge never to" + "\nreveal the secret to anyone."); } //check if answer is correct public static int secret(int [] roll){ int ans = 0; for(int i = 0; i < roll.length; i++){ if(roll[i] == 3) ans += 2; if(roll[i] == 5) ans += 4; } return ans; } //rolling dice public static int[] roll(){ int roll[] = new int[5]; Random generator = new Random(); for(int i = 0; i < 5; i++) roll[i] = generator.nextInt(6) + 1; return roll; } }

This blog entry solves an exercise from Programming Praxis. In the future, please put a link to the original exercise somewhere in the text of your blog entry; it’s proper netiquette to do so, and also required by the Creative Commons BY-NC-SA license under which the exercise is published. Many thanks.

Sure, Thanks.

Thank you for your quick response. I’m glad you enjoy Programming Praxis!