|
| 1 | +package chapter_04; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +/** |
| 6 | + * 10. Write a program that will compute statistics for eight coin tosses. The user |
| 7 | + * will enter either an h for heads or a t for tails for the eight tosses. The program |
| 8 | + * will then display the total number and percentages of heads and tails. Use |
| 9 | + * the increment operator to count each h and t that is entered. For example, a |
| 10 | + * possible sample dialogue between the program and the user might be |
| 11 | + * For each coin toss enter either h for heads or t for tails. |
| 12 | + * First toss: h |
| 13 | + * Second toss: t |
| 14 | + * Third toss: t |
| 15 | + * Fourth toss: h |
| 16 | + * Fifth toss: t |
| 17 | + * Sixth toss: h |
| 18 | + * Seventh toss: t |
| 19 | + * Eighth toss: t |
| 20 | + * Number of heads: 3 |
| 21 | + * Number of tails: 5 |
| 22 | + * Percent heads: 37.5 |
| 23 | + * Percent tails: 62.5 |
| 24 | + * |
| 25 | + * |
| 26 | + * @author Sharaf Qeshta |
| 27 | + * */ |
| 28 | +public class Exercise_04_10 |
| 29 | +{ |
| 30 | + public static void main(String[] args) |
| 31 | + { |
| 32 | + Scanner scanner = new Scanner(System.in); |
| 33 | + System.out.println("For each coin toss enter either h for heads or t for tails."); |
| 34 | + int temp = 0, heads = 0, tails = 0; |
| 35 | + |
| 36 | + System.out.print("First toss: "); |
| 37 | + temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++; |
| 38 | + |
| 39 | + System.out.print("Second toss: "); |
| 40 | + temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++; |
| 41 | + |
| 42 | + System.out.print("Third toss: "); |
| 43 | + temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++; |
| 44 | + |
| 45 | + System.out.print("Fourth toss: "); |
| 46 | + temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++; |
| 47 | + |
| 48 | + System.out.print("Fifth toss: "); |
| 49 | + temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++; |
| 50 | + |
| 51 | + System.out.print("Sixth toss: "); |
| 52 | + temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++; |
| 53 | + |
| 54 | + System.out.print("Seventh toss: "); |
| 55 | + temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++; |
| 56 | + |
| 57 | + System.out.print("Eighth toss: "); |
| 58 | + temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++; |
| 59 | + |
| 60 | + System.out.println("Number of heads: " + heads); |
| 61 | + System.out.println("Number of tails: " + tails); |
| 62 | + System.out.println("Percent heads: " + (heads/8.0) * 100); |
| 63 | + System.out.println("Percent tails: " + (tails/8.0) * 100); |
| 64 | + } |
| 65 | +} |
0 commit comments