11.9k views
0 votes
Create a program to display the result of a coin toss 10 times in Java

A) for (int i = 0; i < 10; i++) { System.out.println("Heads"); }
B) for (int i = 0; i < 10; i++) { System.out.println("Tails"); }
C) for (int i = 0; i < 10; i++) { System.out.println(Math.random() > 0.5 ? "Heads" : "Tails"); }
D) for (int i = 0; i < 10; i++) { System.out.println("Coin"); }

User Corie
by
8.1k points

1 Answer

4 votes

Final answer:

The correct answer is option C) for (int i = 0; i < 10; i++) { System.out.println(Math.random() > 0.5 ? "Heads" : "Tails"); }. This option uses the Math.random() method to generate a random number between 0 and 1 and displays the result of a coin toss 10 times.

Step-by-step explanation:

The correct answer to this question is option C) for (int i = 0; i < 10; i++) { System.out.println(Math.random() > 0.5 ? "Heads" : "Tails"); }

This option uses the Math.random() method to generate a random number between 0 and 1. If the generated number is greater than 0.5, it prints "Heads", otherwise it prints "Tails". This simulates a coin toss and randomly displays the result of a coin toss 10 times.

User Harmic
by
9.3k points