jeudi 3 novembre 2016

How do I make a coin flip race in getting two heads and two tails consecutively in Java

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

Aucun commentaire:

Enregistrer un commentaire