104k views
2 votes
There are 4 numbered instructions in the code, along with

comments that should help you to make changes.
the rest of the code/challenges are posted from me
import java.lang.Math;
class Main {
public static void main(String[] args) {
int max = 9;
int min = 0;
int loops = 3;
int coin1 = 0;
int coin2 = 0;

for (int i =0;i System.out.println("\\#1 Flip 2 Coins");
//CHALLENGE #1 You will notice from the the lines printed below
// that the coins do not have the correct values.
// They should be 0 for tails and 1 for heads.
coin1 = (int)(Math.random()*(max-min)+min);
coin2 = (int)(Math.random()*(max-min)+min);

System.out.println("coin1 =" + coin1);
System.out.println("coin2 =" + coin2);

1 Answer

2 votes
import java.lang.Math;

class Main {
public static void main(String[] args) {
int max = 1;
int min = 0;
int loops = 3;
int coin1 = 0;
int coin2 = 0;

for (int i = 0; i < loops; i++) {
System.out.println("\\#1 Flip 2 Coins");

//CHALLENGE #1 You will notice from the lines printed below
// that the coins do not have the correct values.
// They should be 0 for tails and 1 for heads.
coin1 = (int)(Math.random() * (max - min + 1) + min);
coin2 = (int)(Math.random() * (max - min + 1) + min);

System.out.println("coin1 = " + coin1);
System.out.println("coin2 = " + coin2);

//CHALLENGE #2 Modify the code to count the number of heads flipped.
int numHeads = 0;
if (coin1 == 1) {
numHeads++;
}
if (coin2 == 1) {
numHeads++;
}
System.out.println("Number of heads: " + numHeads);
}
}
}
User Osowskit
by
7.4k points