2014-09-19 45 views
0

所以我對代碼進行了很多修改,現在它符合了,但是我得到了錯誤的總數,並且它總是認爲Player 2在打到「20」之前獲勝。由於某些原因,直到玩家2已經滾動,然後它不計算玩家2 turnTotal,它纔會讀取玩家1 totalScore。當我之前做出改變時,有一件事會開始工作,但另一件事會停止,所以我把它帶回到我編寫之後開始出現問題的地方。Game-Pig-java

import java.util.*; 

public class Proj3Part1 
{ 
public static void main(String[] args) 
{ 
int turnScore = 0; 
int totalScore = 0; 
int turnScore2 = 0; 
int totalScore2 = 0; 
final int WIN = 20; 
int dice = 0; 
int dice2 = 0; 
String input = "r"; 
String input2 = "r"; 
char repeat; 

Scanner keyboard = new Scanner(System.in); 
Scanner s = new Scanner (System.in); 

Random randomNumbers = new Random(); 

while(totalScore < WIN && totalScore2 < WIN) 
{ 
//Player 1's turn 

do 
    { 
     dice = randomNumbers.nextInt(6) + 1; 
     System.out.println(); 
     System.out.println("You rolled: " + dice); 

      if(dice == 1) 
      { 
       turnScore = 0; 
       System.out.println("Turn over."); 
       System.out.println("Player 1 total is " + totalScore); 
       break; 
      } 
      else 
      {   
       turnScore += dice; 
       System.out.print("Player 1 turn total is " + turnScore + " "); 
       System.out.print("Enter (r)oll or (s)top: "); 
       input = keyboard.nextLine(); 
       repeat = input.charAt(0); 


     if(repeat == 's') 
     { 
     System.out.println("Turn over."); 
     System.out.print("Current score: Player 1 has " + totalScore); 
     System.out.println(", Player 2 has " + totalScore2); 
     break; 

     } 
     } 
    } 
while(input.equalsIgnoreCase("r") || dice != 1); 
{    

    totalScore += turnScore; 
} 

    if(totalScore >= WIN) 
    { 
     System.out.println("Your total Score is " + totalScore); 
     System.out.println("Player 1 wins!"); 

    } 


    //player2's turn 
    System.out.println(); 
    System.out.println("It is Player 2's turn."); 

{ do 
    { 
     dice2 = randomNumbers.nextInt(6) + 1; 
     System.out.println("Player 2 rolled: " + dice2); 

     if(dice2 == 1) 
     { 
      turnScore2 = 0; 
      System.out.print("Turn over"); 
      System.out.println("Player 2 total is " + totalScore2); 
      break;    
     } 
     else 
     { 
     turnScore2 += dice2; 
     System.out.print("Player 2 total is " +turnScore2 + " "); 
     System.out.print("Enter (r)oll or (s)top: "); 
     input = keyboard.nextLine(); 
     repeat = input.charAt(0); 


    if(repeat == 's') 
     { 
    System.out.println("Turn over"); 
    System.out.print("Current score: Player 1 has " + totalScore); 
    System.out.println(", Player 2 has " + totalScore2); 
    break; 
     } 
    } 
} 
while(input2.equalsIgnoreCase("r") && dice != 1); { 

    totalScore2 += turnScore2; 

     } 
} 
if(totalScore2 >= WIN); 
    { 
     System.out.println("Player 2 score is " + totalScore2 + "\n"); 
     System.out.println("Player 2 wins"); 
     break; 
    } 
} 


} 
} 
+4

更好地更正縮進和格式以使其更加一致。對於無法正確縮進的用戶,Stackoverflow用戶往往會有偏見。 – 2014-09-19 18:08:37

+6

什麼是編譯器錯誤? – TNT 2014-09-19 18:09:09

+4

請編輯這篇文章的內容,幷包括實際的錯誤是什麼。 – 2014-09-19 18:09:32

回答

2

玩家2 /電腦的回合有一個錯誤。它位於第一個if-else循環中,位於else部分。

input keyboard.nextLine();

應該

input = keyboard.nextLine();

它可以糾正錯誤之後的罰款。

此外,密切關注編譯錯誤,他們會指出你哪些線路產生的錯誤。

修訂:

我認爲這種方式的工作方式,你打算它。

import java.util.*; 

public class Proj3Part1 
{ 
public static void main(String[] args) 
{ 
    int turnScore = 0; 
    int totalScore = 0; 
    int turnScore2 = 0; 
    int totalScore2 = 0; 
    final int WIN = 20; 
    int dice = 0; 
    int dice2 = 0; 
    String input = "r"; 
    String input2 = "r"; 
    char repeat; 

    Scanner keyboard = new Scanner(System.in); 
    Scanner s = new Scanner (System.in); 

    Random randomNumbers = new Random(); 

    while(totalScore < WIN && totalScore2 < WIN) 
    { 
     //Player 1's turn 

     do 
     { 
      dice = randomNumbers.nextInt(6) + 1; 
      System.out.println(); 
      System.out.println("You rolled: " + dice); 

       if(dice == 1) 
       { 
        turnScore = 0; 
        System.out.println("Turn over."); 
        System.out.println("Player 1 total is " + totalScore); 
        break; 
       } 
       else 
       {  
        turnScore = dice; //removed +=??? think it's only the value of dice roll? 
            //either way it's used to compute total, which would be redundant if not 
        totalScore +=turnScore; //added to compute totalScore before turn is over 
        System.out.print("Player 1 turn total is " + totalScore + " "); 
        System.out.print("Enter (r)oll or (s)top: "); 
        input = keyboard.nextLine(); 
        repeat = input.charAt(0); 


        if(repeat == 's') 
        { 
         System.out.println("Turn over."); 
         System.out.print("Current score: Player 1 has " + totalScore); //previously total wasn't computed 
         System.out.println(", Player 2 has " + totalScore2); 
         break; 

        } 
       } 
     }while(input.equalsIgnoreCase("r")); 


     //totalScore += turnScore; was removed + curly braces that seemed to attach it to the above while loop 
     //it isn't needed due to totalScore now being calculated after dice is rolled when !=1(else portion) 

     if(totalScore >= WIN) 
     { 
      System.out.println("Your total Score is " + totalScore); 
      System.out.println("Player 1 wins!"); 
      break; //added to break the loop if player 1 wins 
     } 


     //player2's turn 
     System.out.println(); 
     System.out.println("It is Player 2's turn."); 

     do 
     { 
      dice2 = randomNumbers.nextInt(6) + 1; 
      System.out.println("Player 2 rolled: " + dice2); 

      if(dice2 == 1) 
      { 
       turnScore2 = 0; 
       System.out.print("Turn over"); 
       System.out.println("Player 2 total is " + totalScore2); 
       break;    
      } 
      else 
      { 
       turnScore2 = dice2; //removed += ... same as for player 1's turn 
       totalScore2 += turnScore2; //added totalScore2 calculations. 
       System.out.print("Player 2 total is " +totalScore2 + " "); 
       System.out.print("Enter (r)oll or (s)top: "); 
       input = keyboard.nextLine(); 
       repeat = input.charAt(0); 


       if(repeat == 's') 
       { 
       System.out.println("Turn over"); 
       System.out.print("Current score: Player 1 has " + totalScore);   
       System.out.println(", Player 2 has " + totalScore2); 
       break; 
       } 
      } 
    } 
    while(input2.equalsIgnoreCase("r")); //{ <- incorrect brace + fixed loop for dice2 !=1, then removed it :P since you already did a check inside the do-while loop 

     //totalScore2 += turnScore2; removed, no longer is needed 

      //} 
    //} <- not needed nor is the brace that was infront of the do while loop. 
    if(totalScore2 >= WIN) //removed semicolon since it ended the if statement before it's body 
     { 
      System.out.println("Player 2 score is " + totalScore2 + "\n"); 
      System.out.println("Player 2 wins"); 
      break; 
     } 

    if(totalScore>totalScore2) //added loops to check which score is higher and terminate 
    { 
     System.out.println("Player 1 Wins!"); 
     break; 
    }else if(totalScore==totalScore2){ 
     System.out.println("It's a Tie!"); 
     break; 
    }else if(totalScore<totalScore2){ 
     System.out.println("Player 2 Wins!"); 
     break; 
    } 
    } 


} 
} 

我還建議安裝一個IDE,比如NetbeansEclipse。一個IDE可以讓你的生活更輕鬆,尤其是格式化和語法錯誤。

+0

謝謝!自從發佈此代碼以來,我一直在玩代碼,現在有一套全新的問題:-( – Danielle 2014-09-19 20:40:57

+0

提出更多的問題! – MarGar 2014-09-19 20:41:34

+0

我發佈了修改後的代碼,並解釋了「新」問題。事情會開始工作,但另一個會停止 – Danielle 2014-09-19 21:03:59