-2
public class Pig 
{
 public static void main(String[] args)
 {
  int turnScores = 0;
  int totalScores = 0;
  int turnScores2 = 0;
  int totalScores2 = 0;
  int dice;
  int dice2;
  String input = "r";
  char repeat;

  Scanner keyboard = new Scanner(System.in);

  Random randomNumbers = new Random();

  System.out.println("Welcome to the game of Pig!\n");

  while(totalScores < 100 || totalScores2 < 100)
  {
      //human's turn
      do
      {
          dice = randomNumbers.nextInt(6) + 1;

          System.out.println("You rolled: " + dice);

          if(dice == 1)
          {
              turnScores = 0;
              System.out.print("Your lose your turn!");
              System.out.println("Your Total is " + totalScores);
              break;
          }
          else
          {         
             turnScores += dice;
             System.out.print("Your turn score is " + turnScores);
             System.out.println(" and your total scores is " + totalScores);
             System.out.println("If you hold, you will have " + turnScores 
                               + " points.");
             System.out.println("Enter 'r' to roll again, 'h' to hold.");
             input = keyboard.nextLine();
             repeat = input.charAt(0);

             if(repeat == 'h')
             {
                break;
             }
          }
      }while(input.equalsIgnoreCase("r") || dice != 1);               

         totalScores += turnScores;
         System.out.println("Your scroes is " + totalScores);
      if(totalScores >= 100)
      {
          System.out.println("Your total Scores is " + totalScores);
          System.out.println("YOU WIN!");
          break;            
      }

      //computer's turn
      System.out.println();
      System.out.println("It is the compuer's turn.");
      do
      {
          dice2 = randomNumbers.nextInt(6) + 1; 
          System.out.println("The computer rolled: " + dice2);

          if(dice2 == 1)
          {
              turnScores2 = 0;
              System.out.print("The computer lost its turs!");
              System.out.println(" Computer total is " + totalScores2);
              break;             
          }
          else
          {
              turnScores2 += dice2;
              if(turnScores2 >= 20 || (totalScores2 + turnScores2) >= 100 )
              {
                  System.out.println("The computer holds");
                  break;      
              }
          }
       }while(dice2 != 1 || turnScores2 < 20);
      totalScores2 += turnScores2;
      System.out.println("The computer's scores is " + totalScores2 + "\n");

      if(totalScores2 >= 100);
      {
         System.out.println("THE COMPUTER WINS!");
         break;
      }
  }

 }
}

这些是我的陈述。我不知道为什么我的陈述不循环。我的结果是:

Welcome to the game of Pig!

You rolled: 4
Your turn score is 4 and your total scores is 0
If you hold, you will have 4 points.
Enter 'r' to roll again, 'h' to hold.
h
Your score is 4

It is the computer's turn.
The computer rolled: 6
The computer rolled: 4
The computer rolled: 2
The computer rolled: 5
The computer rolled: 5
The computer holds
The computer's scores is 22

THE COMPUTER WINS!
4

2 回答 2

1

正如我之前所说,您必须删除此语句末尾的分号:

if(totalScores2 >= 100);

如果该语句未删除,则您的 if 语句将被视为关闭语句,跳过,执行 print 语句,然后break执行。在那个阶段,您处于整个while循环中,break 将简单地退出系统

但是,即使修复它也不会使游戏功能完全正确。这是因为您仅turnScores在掷骰子 = 1 时才重置。这是不对的。

如果我的回合得分为 20 并选择保持,则需要将其重置为 0,否则当我开始下一次回合时,我已经有 20 分开始了。

解决方案是在回合结束时的分数打印语句之后重置 turnScore,如下所示:

totalScores += turnScores;
System.out.println("Your scroes is " + totalScores);
turnScores = 0;

然后对于计算机:

totalScores2 += turnScores2;
System.out.println("The computer's scores is " + totalScores2 + "\n");
turnScores2 = 0;
于 2013-08-18T11:36:23.640 回答
0

我花了很长时间才注意到if(totalScores2 >= 100);有“;” 到底。删除它,否则 if 语句没有意义

于 2013-08-18T11:30:00.023 回答