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

comments that should help you to make changes.


//CHALLENGE #3 Change the code below to say WOW if at least 1 coin is heads (1, not 0)
// You only need to change the conditions.
if (( coin1 !=3 ) && ( coin2 >= 4)){
System.out.println("WOW! At least one coin is heads");
} else {
System.out.println("neither coin is heads");
}

//CHALLENGE #4 Change the code below to say WOW if both coins are heads
// You only need to change the conditions.
if (( coin1 != 1) || (coin2 <= 3)){
System.out.println("WOW! Both coins are Heads!");
} else {
System.out.println("No matches");
}
}
}
}

User Jin Thakur
by
8.6k points

1 Answer

3 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);

//CHALLENGE #3 Change the code to say WOW if at least 1 coin is heads
if (coin1 == 1 || coin2 == 1) {
System.out.println("WOW! At least one coin is heads");
} else {
System.out.println("Neither coin is heads");
}

//CHALLENGE #4 Change the code to say WOW if both coins are heads
if (coin1 == 1 && coin2 == 1) {
System.out.println("WOW! Both coins are Heads!");
} else {
System.out.println("No matches");
}
}
}
}
User Alexandros
by
8.7k points