简体   繁体   English

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

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

First of all, I have found two other threads that have similar questions. 首先,我发现了另外两个具有类似问题的线程。 There problem is that they didn't use the right equal sign for string and not formatting the if statements correctly for their particular problem. 问题在于,他们没有为字符串使用正确的等号,也没有为特定问题正确格式化if语句

For my assignment, I need to create a game called Pig where the player verses a computer in getting 100 points first in rolling pairs of dice. 对于我的任务,我需要创建一个名为Pig的游戏,该游戏中的玩家精通计算机,在掷骰子对中首先获得100分。 If the player rolls 1 on one turn, they get no additional points. 如果玩家一圈掷出1,则不会获得额外的积分。 If the player rolls two 1's, then they lose all their points. 如果玩家掷出两个1,那么他们将失去所有积分。 I have not coded the computer's turn yet, just focusing on the player. 我还没有编码计算机的转向,只是专注于播放器。 Please tell me what I am doing wrong. 请告诉我我在做什么错。 Thank you very much in advance. 提前非常感谢您。

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

My Output: 我的输出:

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

If num1 is 1, then the first if condition takes it. 如果num1为1,则第一个if条件接受它。 It will not check the 'else if' condition. 它不会检查“否则”条件。 Similarly, if num2 is 1, the if condition takes it. 同样,如果num2为1,则if条件接受它。 So put your && condition first. 因此,将您的&&条件放在首位。

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

Your if logic is flawed and a bit redundant. 您的if逻辑有缺陷并且有点多余。 Try this: 尝试这个:

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