首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的代码只打印3或7以外的任何内容?

为什么我的代码只打印3或7以外的任何内容?
EN

Stack Overflow用户
提问于 2019-09-23 23:35:00
回答 4查看 61关注 0票数 1

我正在尝试建立一个掷骰子游戏,其中如果计算机自动掷一对骰子,如果cpu掷出7或11,用户获胜。但是,如果用户掷出2、3或12,他们会自动输掉。如果用户滚动任何其他数字(4,5,6,8,9,10),这是“点”,他们将尝试再次滚动该点。(除非他们掷出7,否则他们会输。)我正在尝试让我的while循环继续滚动,如果计算机滚动的数字不是7或"point“,唯一运行的是”恭喜你滚动了7你赢了“或”你已经滚动了3你所爱的“。

这就是我现在拥有的..

代码语言:javascript
运行
复制
public class CrapsPractice {
    public static void main(String[] args) {

        int d1 = (int) (6 * Math.random() + 1);
        int d2 = (int) (6 * Math.random() + 1);
        int roll = (d1 + d2);
        int point = roll;

        if (roll == 7 || roll == 11)

        {
            System.out.println("You rolled a" + roll);
            System.out.println("Congrats! You've immediately won!");

        }

        else if (roll == 2 || roll == 3 || roll == 12) {
            System.out.println("you rolled a " + roll);
            System.out.println("You lose!");

            while (roll != 7 && roll != point) {
                int d3 = (int) (6 * Math.random() + 1);
                int d4 = (int) (6 * Math.random() + 1);
                roll = d3 + d4;

                System.out.println("your point is" + point);

            }
        }

    }

}
EN

回答 4

Stack Overflow用户

发布于 2019-09-23 23:44:28

您的花括号放错了位置。在while启动后,您正在关闭您的else if。您应该将格式重新设置为如下所示:

代码语言:javascript
运行
复制
public class crapsPractice{
public static void main(String[]args){   


   int d1 = (int) (6 * Math.random() + 1);
   int d2 = (int) (6 * Math.random() + 1);
   int roll = (d1 + d2);
   int point = roll; 




       if (roll == 7 || roll == 11) 

       {
         System.out.println("You rolled a" + roll); 
         System.out.println("Congrats! You've immediately won!");

       }

       else if (roll == 2 || roll == 3 || roll == 12)
       {
         System.out.println("you rolled a " + roll);
         System.out.println("You lose!");
       }

    while(roll != 7 && roll != point )
    {
    int d3 = (int) (6 * Math.random() + 1);
    int d4 = (int) (6 * Math.random() + 1);
    roll = d3 + d4;

    System.out.println("your point is" + point);

    }


}


} 
票数 1
EN

Stack Overflow用户

发布于 2019-09-23 23:46:15

NotZack是对的,检查你的括号,但你也有这个:

while(roll != 7 && roll != point )

rollpoint不是相同的吗,所以它永远不会运行你的while循环

票数 0
EN

Stack Overflow用户

发布于 2019-09-23 23:52:45

您忘记添加else部分,这就是为什么不打印其他数字的原因。此外,类名始终以大写字母开头。

代码语言:javascript
运行
复制
public class CrapsPractice {
        public static void main(String[] args) {

            int d1 = (int) (6 * Math.random() + 1);
            int d2 = (int) (6 * Math.random() + 1);
            int roll = (d1 + d2);
            int point = roll;

            if (roll == 7 || roll == 11)

            {
                System.out.println("You rolled a" + roll);
                System.out.println("Congrats! You've immediately won!");

            }

            else if (roll == 2 || roll == 3 || roll == 12) {
                System.out.println("you rolled a " + roll);
                System.out.println("You lose!");

                while (roll != 7 && roll != point) {
                    int d3 = (int) (6 * Math.random() + 1);
                    int d4 = (int) (6 * Math.random() + 1);
                    roll = d3 + d4;

                    System.out.println("your point is" + point);

                }
            }
            else 
            {
                System.out.println("your point is " + point);
            }

        }

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58065787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档