繁体   English   中英

如果else语句不执行怎么办?

[英]How come my if else statements don't execute?

首先,我发现了另外两个具有类似问题的线程。 问题在于,他们没有为字符串使用正确的等号,也没有为特定问题正确格式化if语句

对于我的任务,我需要创建一个名为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

如果num1为1,则第一个if条件接受它。 它不会检查“否则”条件。 同样,如果num2为1,则if条件接受它。 因此,将您的&&条件放在首位。

        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");

您的if逻辑有缺陷并且有点多余。 尝试这个:

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM