2016-02-12 120 views
0

遊戲的人的部分似乎工作正常,但只要我輸入「S」結束玩家的轉碼進入與電腦滾動無限循環。如果一回合結束,它不會循環。有人能指出我錯過了什麼嗎?提前致謝!與玩家vs電腦豬遊戲循環問題

import java.util.Random; 
import java.util.Scanner; 
public class Proj3Part2 { 
public static void main(String[] args) { 

    int playerTurnScore = 0; 
    int playerTotScore = 0; 
    int compTotScore = 0; 
    int compTurnScore = 0; 
    int turn = 1; 
    int dice = 0; 
    int counter = 0; 
    Scanner input = new Scanner (System.in); 
    Random roll = new Random(); 
    final int WIN = 20; 
    char decision = 'r'; 
    boolean gameOver = false; 
    boolean human = true; 

    System.out.print("Your turn total is: " + playerTurnScore + ". Enter <r>oll or <s>top: "); 
    decision = input.next().charAt(0); 
while (gameOver == false) {   
while (turn == 1) {    
    if (decision == 'r' || decision == 'R') { 
     dice = (int)(Math.random()*6) + 1; 
     System.out.println("You rolled " + dice);    
     if (dice == 1) { 
       System.out.println("Turn over."); 
       System.out.println("\nCurrent score: You have " + playerTotScore + ", Computer has " + compTotScore); 
       playerTurnScore = 0; 
       turn = 2; 
      }     
      else { 
       playerTurnScore +=dice; 
       System.out.print("Your turn total is " + playerTurnScore + ". Enter <roll> or <s>top "); 
       decision = input.next().charAt(0);     

       }     
      }    
    else { 
       System.out.print("Turn over."); 
       System.out.println("\nCurrent score: You have " + (playerTotScore += playerTurnScore) + ", Computer has " + compTotScore); 
       playerTurnScore = 0; 
       turn = 2; 

      }  
    if (playerTotScore >= WIN) { 
      System.out.println("\nYou win!"); 
      gameOver = true;    
      }    
} 

while (turn == 2) { //computer turn 
     System.out.print("Computer turn total is: " + compTurnScore); 
     System.out.println(" Computer rolls."); 
     dice = (int)(Math.random()*6) + 1; 
     System.out.println("Computer rolled: " + dice); 
     counter++; 
    if (dice == 1) { 
       System.out.print("Turn over."); 
       System.out.println("\nCurrent score: You have " + playerTotScore + ", Computer has " + compTotScore); 
       compTurnScore = 0; 
       turn = 1; 
       break; 
      }   
    if (counter < 3) { 
       compTurnScore += dice; 
       dice = (int)(Math.random()*6) + 1;  
       counter++; 
      }     
     if (counter >= 3) { 
      compTurnScore += dice; 
      System.out.print("Turn over."); 
      compTotScore += compTurnScore; 
      System.out.println("\nCurrent score: You have " + playerTotScore + ", Computer has " + compTotScore); 
      turn = 1; 
      break; 
    }   
} 
} 
System.exit(0); 
} 
} 

回答

0

通過在調試器中調試代碼,我可以看到你在第一循環有

System.out.print("Your turn total is " + playerTurnScore + ". Enter <roll> or <s>top "); 
decision = input.next().charAt(0); 

所以它要求下一步做什麼,但你不要在你的第二個循環都沒有了。

+0

我感到困惑如何添加,自電腦的回合應該不需要任何外部輸入是否正確? –

+0

@NikkiHarding然而,在計算機滾動後,你需要問人類接下來會發生什麼,或者你有一個無限循環。 –

+1

好的,那修復了這個循環。謝謝你解釋! –