-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ループにいるので、ブレークは単にシステムを終了します

ただし、それを修正しても、ゲームの機能はまったく正しくありません。これはturnScores、サイコロの出目が 1 の場合にのみ をリセットするためです。これは正しくありません。

ターン スコアが 20 でホールドすることを選択した場合、0 にリセットする必要があります。

解決策は、次のように、ターン終了時にスコアの print ステートメントの直後に 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 に答える