Write a Java program Name FlipMyCoin that simulates flipping
Solution
FlipMyCoin.java
import java.util.Scanner;
public class FlipMyCoin {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(\"***Welcome to Flip My Coin***\");
while(true){
System.out.print(\"Enter how many items to flip the coin: \");
int n = scan.nextInt();
int heaCount =0, tailCount = 0;
for(int i=0; i<n; i++){
int toss = (int )(Math. random() * 2);
if(toss == 1){
System.out.println(\"H\");
heaCount++;
}
else{
System.out.println(\"T\");
tailCount++;
}
}
System.out.println(\"Head Count: \"+heaCount);
System.out.println(\"Tail Count: \"+tailCount);
System.out.print(\"Do you want to continue (y or n): \");
char ch = scan.next().charAt(0);
if(ch == \'n\'|| ch==\'N\'){
break;
}
}
}
}
Output:
***Welcome to Flip My Coin***
Enter how many items to flip the coin: 10
H
H
H
T
T
H
T
T
H
H
Head Count: 6
Tail Count: 4
Do you want to continue (y or n): y
Enter how many items to flip the coin: 5
T
H
H
T
H
Head Count: 3
Tail Count: 2
Do you want to continue (y or n): n

