如何在 Java 中连续获得两个正面和两个反面进行抛硬币比赛

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

我刚开始使用比主要方法更复杂的方法进行编码。我被分配到用三个硬币进行比赛。无论哪个硬币先抛出 2 个正面和 2 个反面,谁就获胜。我编写了一个 if else 语句来确定哪个硬币获胜,但是这两个 if 语句都没有被执行。如果您在我的 if else 语句或其他地方看到错误,请告诉我。我还需要包含其他方法的其他代码程序。

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!");


 }
}

这是我的输出:

    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

尝试将比较更改为

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!");

当然,另一种方法是简单地调试代码并检查值

我不明白你的代码是如何解决这个问题的。如果我没理解错的话,你想要第一个显示组合 H-H-T-T 的硬币。现在,您可以按任何顺序计算正面和反面。所以,如果第一枚硬币有 H-T-H-T,第二枚和第三枚分别有 H-H-H-T 和 H-T-T-T,则第一个获胜。

为了解决正面和反面顺序的问题,我认为你应该改变每个硬币的if-else语句(我这里只针对coin1):

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;
  }
}

您还应该更改 while 语句...当任何硬币出现两个正面和两个反面时,您想停止。所以,它会是这样的: while (!(count1 == 2 && count10 == 2) && !(count2 == 2 && count20 == 2) && ....) {...}