2016-05-18 161 views
0

所以,我正在爲這個課程進行這項任務。這是一個Java類,我應該製作一個遊戲,它會擲出兩個骰子,將它們加起來,並將它們添加到輪到分數中。然後問你是否想繼續玩。當你的回合得分達到20分,或者你決定通過時,它就會進入電腦。它應該打印每個回合的分數,然後當有人擊中100分時,它宣佈獲勝者。然而,無論如何,每回合結束時的得分是0,無論我運行多少次。當玩家擲出1時,他們的回合得分被取消,並且移動到另一個玩家,並且如果他們擲出雙倍1,則他們目前失去了他們的所有積分。這是我的代碼,你能找出爲什麼分數變量不更新嗎?謝謝。無法弄清楚爲什麼它不能正確解析

import java.util.Scanner; 
import java.util.Random; 
public class PlayPig { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Scanner scan = new Scanner(System.in); 
     int player1 = 0; 
     int player2 = 0; 
     int a, b, c, player1turn, player2turn, input; 
     int pig = 1; 
     Random r = new Random(); 
     do{ 
      do { 
       player1turn=0; 
       a = r.nextInt(6)+1; 
       b = r.nextInt(6)+1; 
        if(a==1 || b==1){ 
         if (a == 1 && b == 1){ 
          player1=0; 
          break;} 
         else if (a==1 || b==1){ 
          player1turn=0; 
          break;} 
        else { 
         player1turn= a+b ; 
         }} 
        player1= player1+player1turn; 
       System.out.println("Player1 score is " + player1 + " and player2 score is " + player2); 
       System.out.print("Do you want to keep playing? Enter 1 to continue. Enter any other number to pass."); 
       input = scan.nextInt(); 
        if (input != 1) 
         break; 
      } 
      while  
       (player1turn <= 20); 
      do{ 
       player2turn=0; 
       a = r.nextInt(6)+1; 
       b = r.nextInt(6)+1; 
       if(a==1 || b==1){ 
        if (a == 1 && b == 1){ 
         player2=0; 
         break;} 
        else if (a==1 || b==1){ 
         player2turn=0; 
         break;} 
       else { 
        player1turn= a+b ; 
        player2= player2+player2turn;}} 
      } 
      while 
       (player2turn<=20); 
     } 
     while 
      (player1 < 100 || player2 < 100); 
     if (player1>player2) 
      System.out.print("Player 1 wins"); 
     else 
      System.out.print("Player 2 wins"); 

}} 
+0

在您使用的程序的一部分player2'player1turn = A + B'當你想用'player2turn = A + B' –

回答

1

主要問題是,您指定當前得分的else條件在錯誤的塊中。 (這些的):

else { 
    player1turn = a+b ; 
} 

試試這個代碼:

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    Scanner scan = new Scanner(System.in); 
    int player1 = 0; 
    int player2 = 0; 
    int a, b, c, player1turn, player2turn, input;   
    int pig = 1; 
    Random r = new Random(); 
    do{ 

     do { 

      player1turn=0; 
      a = r.nextInt(6)+1; 
      b = r.nextInt(6)+1; 
       if(a==1 || b==1){ 
        if (a == 1 && b == 1){ 
         player1 = 0; 
         break; 
        } 
        else if (a==1 || b==1){ 
         player1turn=0; 
         break; 
        } 
       }else { 
        player1turn = a+b ; 
       } 
      player1 += player1turn; 
      System.out.println("Player1 score is " + player1 + " and player2 score is " + player2); 
      System.out.print("Do you want to keep playing? Enter 1 to continue. Enter any other number to pass."); 
      input = scan.nextInt(); 
       if (input != 1){ 
        break; 
       } 
     } while (player1turn <= 20); 

     do{ 
      player2turn=0; 
      a = r.nextInt(6)+1; 
      b = r.nextInt(6)+1; 

      if(a==1 || b==1){ 
       if (a == 1 && b == 1){ 
        player2=0; 
        break; 
       } else if (a==1 || b==1){ 
        player2turn=0; 
        break; 
       }    
      }else { 
       player2turn = a+b ; 
       player2 += player2turn; 
      } 

     }while (player2turn<=20); 

    } while (player1 < 100 || player2 < 100); 
    if (player1>player2) 
     System.out.print("Player 1 wins"); 
    else 
     System.out.print("Player 2 wins"); 

} 
0

我已經修改了,如果循環。 你可以試試這個:

import java.util.Scanner; 
import java.util.Random; 
public class PlayPig { 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    Scanner scan = new Scanner(System.in); 
    int player1 = 0; 
    int player2 = 0; 
    int a, b, c, player1turn, player2turn, input; 
    int pig = 1; 
    Random r = new Random(); 
    do{ 
     do { 
      player1turn=0; 
      a = r.nextInt(6)+1; 
      b = r.nextInt(6)+1; 
       if (a == 1 && b == 1){ 
         player1=0; 
         break; 
       } 
       else if((a== 1 && b!= 1) || (a!=1 && b== 1){ 
        player1turn=0; 
        break; 
       } 
       else{ 
        player1turn= a+b ; 
       } 
       player1= player1+player1turn; 
      System.out.println("Player1 score is " + player1 + " and player2   score is " + player2); 
      System.out.print("Do you want to keep playing? Enter 1 to  continue. Enter any other number to pass."); 
      input = scan.nextInt(); 
       if (input != 1) 
        break; 
     } 
     while  
      (player1turn <= 20); 
     do{ 
      player2turn=0; 
      a = r.nextInt(6)+1; 
      b = r.nextInt(6)+1; 
      if (a == 1 && b == 1){ 
         player2=0; 
         break; 
       } 
       else if((a== 1 && b!= 1) || (a!=1 && b== 1){ 
        player2turn=0; 
        break; 
       } 
       else{ 
        player2turn= a+b ; 
       } 
       player2= player2+player2turn; 
     } 
     while 
      (player2turn<=20); 
    } 
    while 
     (player1 < 100 || player2 < 100); 
    if (player1>player2) 
     System.out.print("Player 1 wins"); 
    else 
     System.out.print("Player 2 wins"); 

}} 
相關問題