I just started coding with more complex methods than the main method. I was given an assignment to make a race with three coins. Whichever coin flips 2 heads and 2 tails first in that order wins. I coded an if else statement to determine which coin wins but neither of the if statements are ever executed. Please tell me if you see an error in my if else statements or somewhere else. I also have to other programs of code that include other methods.
public class FlipRace
{
public static void main (String[] args)
{
final int GOALHEAD = 2;
final int GOALTAIL = 2;
int count1 = 0, count2 = 0, count3 = 0, count10 = 0, count20 = 0, count30 = 0;
// Create three separate coin objects
Coin coin1 = new Coin();
Coin coin2 = new Coin();
Coin coin3 = new Coin();
while (count1 <= GOALHEAD && count10 <= GOALTAIL || count2 <= GOALHEAD && count20 <= GOALTAIL || count3 <= GOALHEAD && count30 <= GOALTAIL)
{
coin1.flip();
coin2.flip();
coin3.flip();
// Print the flip results (uses Coin's toString method)
System.out.print ("Coin 1: " + coin1);
System.out.println (" Coin 2: " + coin2);
System.out.println (" Coin 3: " + coin3);
// Increment or reset the counters
if (coin1.isHeads())
count1++;
else
count10++;
if (coin2.isHeads())
count2++;
else
count20++;
if (coin3.isHeads())
count3++;
else
count30++;
}
// Determine the winner
if (count1 == GOALHEAD && count10 == GOALTAIL)
System.out.println ("Coin 1 wins!");
else if (count2 == GOALHEAD && count20 == GOALTAIL)
System.out.println ("Coin 2 wins!");
else if (count3 == GOALHEAD && count30 == GOALTAIL)
System.out.println ("Coin 3 wins!");
else
System.out.println ("It's a TIE!");
}
}
Here is my output:
Coin 1: Heads Coin 2: Heads
Coin 3: Tails
Coin 1: Heads Coin 2: Heads
Coin 3: Heads
Coin 1: Heads Coin 2: Tails
Coin 3: Heads
Coin 1: Heads Coin 2: Heads
Coin 3: Tails
Coin 1: Heads Coin 2: Tails
Coin 3: Heads
It's a TIE!// this message comes up every time because something is wrong
try changing your comparison to
if (count1 >= GOALHEAD && count10 >= GOALTAIL)
System.out.println ("Coin 1 wins!");
else if (count2 >= GOALHEAD && count20 >= GOALTAIL)
System.out.println ("Coin 2 wins!");
else if (count3 >= GOALHEAD && count30 >= GOALTAIL)
System.out.println ("Coin 3 wins!");
else
System.out.println ("It's a TIE!");
of course another way would to simply debug your code and inspect the values
I don't understand how your code would solve the problem. If I understand it right, you want the first coin that shows the combination HHTT. Now, you are counting the heads and tails in ANY order. So, if you have HTHT for the first coin and HHHT and HTTT for the second and third respectively, the first one wins.
In order to solve the problem considering the order of heads and tails, I think you should change the if-else statement for each coin (I'll make it only for coin1 here):
if (coin1.isHeads()) {
if (count1 < 2 && count2 == 0) { //less than 2 heads and zero tails
count1++;
} else {
count1 = 0;
count10 = 0;
}
} else { //tails
if (count1 == 2 && count10 < 2) { //we already have two heads and 0 or 1 tail
count10++;
} else { // either less than two heads or too many tails - we have to restart!
count1 = 0;
count10 = 0;
}
}
You should also change the while statement... You want to stop when you have two heads ant two tails for any coin. So, it'd be something like this: while (!(count1 == 2 && count10 == 2) && !(count2 == 2 && count20 == 2) && ....) {...}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.