0

我刚开始使用比主要方法更复杂的方法进行编码。我被分配用三个硬币进行比赛。哪个硬币按顺序先翻转 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
4

2 回答 2

0

尝试将您的比较更改为

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

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

于 2016-11-04T01:04:57.380 回答
0

我不明白您的代码将如何解决问题。如果我理解正确,您想要第一个显示 HHTT 组合的硬币。现在,您可以按任何顺序计算正面和反面。因此,如果第一枚硬币有 HTHT,第二枚和第三枚硬币分别有 HHHT 和 HTTT,则第一个获胜。

为了解决考虑正面和反面顺序的问题,我认为您应该更改每个硬币的 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) && ....) {...}

于 2016-11-04T01:09:06.547 回答