2013-08-18 37 views
-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; 
     } 
    } 

} 
} 

這些都是我的陳述。我不知道爲什麼我的陳述不循環。 我的結果是:PIG遊戲Java代碼;你可以幫我嗎?

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! 
+1

首先,使用Javascript不是Java - 請從您的問題中刪除該標籤。 。 –

+0

你最後的if語句(如果(totalScores2> = 100),是因爲你有後分號封閉刪除 –

+3

我滾的問題回來,如果有人幫你,接受他們的回答;不破壞原來的問題。 – Makoto

回答

0

我花了永遠的通知if(totalScores2 >= 100);有 「;」到底。刪除或以其他方式if語句是沒有意義的

1

正如我剛纔所說,你必須從該聲明的末尾刪除分號:

if(totalScores2 >= 100); 

如果語句不刪除,你的if語句作爲一個封閉的聲明中,跳過,執行打印語句,然後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;