Skip to content

Commit c5756fd

Browse files
committedMar 11, 2024·
finished exercise 40
1 parent 01d3d3e commit c5756fd

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed
 

‎src/com/github/jonathanbirkey/chapter05/Exercise40.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22
* @author : Jonathan Birkey
33
* @mailto : jonathan.birkey@gmail.com
44
* @created : 28Feb2024
5-
* <p>(Display leap years) Write a program that displays all the leap years, 10 per line, from
6-
* 101 to 2100, separated by exactly one space. Also display the number of leap years in this
7-
* period.
5+
* <p>(Simulation: heads or tails) Write a program that simulates flipping a coin one million
6+
* times and displays the number of heads and tails.
87
*/
98
package com.github.jonathanbirkey.chapter05;
109

1110
public class Exercise40 {
1211
public static void main(String[] args) {
13-
// TODO: solve
12+
int coin = 0;
13+
int heads = 0;
14+
int tails = 0;
15+
16+
for (int i = 0; i < 1000000; i++) {
17+
coin = (int)(Math.random()*2);
18+
if(coin == 1) {
19+
heads++;
20+
}else {
21+
tails++;
22+
}
23+
}
24+
System.out.printf("Heads: %d\nTails: %d",heads, tails);
1425
}
1526
}

0 commit comments

Comments
 (0)
Please sign in to comment.