205k views
5 votes
java Write a program that simulates flipping a coin to make decisions. The input is how many decisions are needed, and the output is either heads or tails. Assume the input is a value greater than 0. Ex: If the input is: 3 the output is: tails heads tails

1 Answer

3 votes

Answer:

import java.util.Scanner;

public class num9 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("How many decisions are needed");

int dec = in.nextInt();

for(int i =0; i<dec; i++){

if(Math.random()<0.5){

System.out.print("Heads ");

}

else{

System.out.print("Tails ");

}

}

}

}

Step-by-step explanation:

The Program is implemented in Java as required

Use scanner class to receive and save the variable decisions that indicates the number of times for the tosin of the coin

Use a for loop that will iterate from 0 to the number of times entered by the user

Within the for loop, use Java's Math.random method to randomly generate numbers

use an if condition to determine the output of Heads or Tails depending on the random number generated

see code and output attached

java Write a program that simulates flipping a coin to make decisions. The input is-example-1
User Dickeylth
by
5.0k points