Java 为什么我的if-else语句不';不执行?

Java 为什么我的if-else语句不';不执行?,java,loops,if-statement,Java,Loops,If Statement,首先,我发现另外两个线程也有类似的问题。问题是他们没有使用,也不是为了他们的特殊问题 在我的作业中,我需要创建一个名为“猪”的游戏,玩家与计算机对决,在掷骰子时先得到100分。如果玩家在一个回合中掷1,他们不会得到额外的分数。如果玩家掷两个1,那么他们将失去所有分数。我还没有编码计算机的回合,只是专注于球员。请告诉我我做错了什么。事先非常感谢 import java.util.Scanner; public class FourFive { public static void main

首先,我发现另外两个线程也有类似的问题。问题是他们没有使用,也不是为了他们的特殊问题

在我的作业中,我需要创建一个名为“猪”的游戏,玩家与计算机对决,在掷骰子时先得到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();


如果num1是1,那么第一个If条件取它。它不会检查“else 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");

如果num1是1,那么第一个If条件取它。它不会检查“else 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);

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

如果第一个条件为真,则它将永远不会转到第二个条件…任何If-else语句都不会执行,因为除了If和else-If检查的顺序之外,无论发生什么情况,您也会将点分配给playerTotal。>玩家总数+=点数;这不在任何检查范围内。如果第一个条件为真,它将永远不会进入第二个条件…任何If-else语句都不会执行,因为除了If和else-If检查的顺序之外,无论发生什么情况,您也会将点分配给playerTotal。>玩家总数+=点数;这不在任何检查范围内。我刚刚遇到了那团乱麻,谢谢你修复代码。我刚刚遇到了那团乱麻,谢谢你修改代码他实际上必须设置
点数=0
而不是
+=
,因为他已经对骰子求和了。他实际上必须设置
点数=0
而不是
+=
,因为他已经对骰子求和了。