2013-09-26 34 views
0

如果我不滚动1,我想“保持”我的滚动。该代码忽略“h”括号内条件语句内的指令,并再次掷骰子。我很困惑,因为如果我滚动1,代码确实工作并转向计算机,正确设置标志。循环和条件不正常

while ((humanScore <= 100) && (computerScore <=100)) 
    { 
     /*loop while human turn is true*/ 
     while ((humanTurn == true) && (computerTurn == false)) 
     { 
      die = randomGenerator.nextInt(6) + 1; 

      if(die == 1) 
      { 
       System.out.println("Human, you rolled a 1, you lose your points and your turn."); 
       humanTurn = false; 
       computerTurn = true; 
       points = 0; 
       System.out.println("Your score is now " + humanScore); 
       System.out.println("Computer, it is now your turn."); 
      } 

      else if(die != 1) 
      { 
       System.out.println("Human, you currently have " + points + " points to add to score."); 
       System.out.println("You have rolled a " + die + " would you like to hold, or roll again?"); 
       System.out.println("Please enter either r to roll, or h to hold."); 
       points = points + die; 
       decision = scanner.next(); 

       if (decision == "r") 
       { 
        humanTurn = true; 
       } 

       if(decision == "h") 
       { 
        humanScore = humanScore + points; 
        humanTurn = false; 
        computerTurn = true; 
        points = 0; 
        System.out.println("You hold, your score is now " + humanScore); 
        System.out.println("Computer, it is now your turn."); 
       } 

      } 
     } 
+15

使用'String.equals'比较'Strings' – Reimeus

+4

如上所述,我还删除了布尔humanTurn/computerturn,使用简单humanTurn虚假意味着计算机转 –

回答

0
if (decision.equals("r")) 
{ 
    humanTurn = true; 
} 

if(decision.equals("h")) 
{ 
     humanScore = humanScore + points; 
     humanTurn = false; 
     computerTurn = true; 
     points = 0; 
     System.out.println("You hold, your score is now " + humanScore); 
     System.out.println("Computer, it is now your turn."); 
}