簡體   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