我的if else语句怎么不执行?

时间:2016-11-15 04:46:48

标签: java loops if-statement

首先,我发现了另外两个有类似问题的线程。问题在于他们没有使用right equal sign for string而不是formatting the if statements correctly来解决他们的特定问题。

对于我的任务,我需要创建一个名为Pig的游戏,其中玩家在计算机中首先在滚动的骰子中获得100分。如果玩家在一个回合中滚动1,则他们不会获得额外的积分。如果玩家掷出两个1,那么他们将失去所有积分。我还没有对计算机进行编码,只关注播放器。请告诉我我做错了什么。非常感谢你提前。

import java.util.Scanner;
public class FourFive
{
    public static void main (String[] args)
    {
    Pigs myopp = new Pigs();
    Scanner scan = new Scanner (System.in);
    final int Round = 20;
    int num1, num2;

    int roundTotal = 0;
    int playerTotal = 0;
    int compTotal = 0;
    int win = 100;
    int turnOver = 1;
    Pigs die1 = new Pigs();
    Pigs die2 = new Pigs();
    String play = "y";
    System.out.println("Type y to play");
    play = scan.nextLine();
    while (play.equalsIgnoreCase("y"))
    {
        for (int roll = 1; roll <= 6; roll++)//Each die has 6 sides
        {
            num1 = die1.roll();//rolls first die
            num2 = die2.roll();//rolls second die
            int points = num1 + num2;// adds dies up to get total for this turn
            System.out.println("You roll " + num1 + " and " + num2);

            if (num1 == 1 || num2 == 1)//If either of the dies roll 1, no points 
                points += 0;
            else if (num1 == 1 && num2 == 1)//if both are 1, lose ALL points
                playerTotal = 0;
            else
                System.out.println("you earned " + points + " this round");

            playerTotal += points; total number of points per round added   
            System.out.println("You have a total of " + playerTotal);


            System.out.println("Type y to play");
            play = scan.nextLine();
      }
    }
 }
}

我的输出:

Type y to play
y
You roll 4 and 2
you earned 6 this round
You have a total of 6
Type y to play
y
You roll 6 and 5
you earned 11 this round
You have a total of 17
Type y to play
y
You roll 1 and 1
You have a total of 19 //total should be 0 because of the two 1's
Type y to play
y
You roll 6 and 3
you earned 9 this round
You have a total of 28
Type y to play
y
You roll 1 and 1
You have a total of 30 //total should be 0 because of the two 1's
Type y to play
y
You roll 6 and 4
you earned 10 this round
You have a total of 40
Type y to play
y
You roll 5 and 2
you earned 7 this round
You have a total of 47
Type y to play
y
You roll 5 and 1
You have a total of 53 //shouldn't add any additional points because of the "1"
Type y to play

2 个答案:

答案 0 :(得分:2)

如果num1为1,则第一个if条件接受它。它不会检查&#39;否则是否&#39;条件。同样,如果num2为1,则if条件接受它。所以把你的&amp;&amp;条件优先。

        if (num1 == 1 && num2 == 1)//if both are 1, lose ALL points
            playerTotal = 0;
        else if (num1 == 1 || num2 == 1)//If either of the dies roll 1, no points 
            points += 0;
        else
            System.out.println("you earned " + points + " this round");

答案 1 :(得分:0)

你的逻辑是否有缺陷并且有点多余。试试这个:

if (num1 == 1 && num2 == 1) {
    playerTotal = 0;
}
else if (num1 != 1 && num2 != 1) {
    playerTotal += points;
    System.out.println("you earned " + points + " this round");
}
System.out.println("You have a total of " + playerTotal);