循环和条件不正常

时间:2013-09-26 15:52:04

标签: java

如果我不掷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.");
                }

            }
        }

1 个答案:

答案 0 :(得分: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.");
 }