File tree 1 file changed +15
-4
lines changed
src/com/github/jonathanbirkey/chapter05
1 file changed +15
-4
lines changed Original file line number Diff line number Diff line change 2
2
* @author : Jonathan Birkey
3
3
* @mailto : jonathan.birkey@gmail.com
4
4
* @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.
8
7
*/
9
8
package com .github .jonathanbirkey .chapter05 ;
10
9
11
10
public class Exercise40 {
12
11
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\n Tails: %d" ,heads , tails );
14
25
}
15
26
}
You can’t perform that action at this time.
0 commit comments