Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的代码只打印3或7以外的内容?_Java - Fatal编程技术网

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

Java 为什么我的代码只打印3或7以外的内容?,java,Java,我试图建立一个骰子游戏,如果计算机自动掷一对骰子,如果cpu掷7或11,用户获胜。但是,如果用户掷2、3或12,它们将自动丢失。如果用户滚动任何其他数字(4、5、6、8、9、10)即为“点”,他们将尝试再次滚动该点。(除非他们掷出7,否则他们会输。)如果计算机掷出的数字不是7或“点”,我会尝试让我的while循环继续掷出。唯一运行的是“恭喜你掷出了7,你赢了”或“你掷出了3,你喜欢” 这就是我现在拥有的 public class CrapsPractice { public static

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

这就是我现在拥有的

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

            }
        }

    }

}

你的大括号放错位置了。如果在启动后关闭,则为否则。您应该重新格式化为以下格式:

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

    }


}


} 

诺扎克是对的检查你的括号,但你也有:

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


滚动
不相等,因此它永远不会运行while循环

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

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

        }

    }

正如许多评论/答案所指出的那样,这里你肯定有一个大括号

另外,当用户立即获胜或失败时,我假设您希望停止执行,而不是进入while循环。为此,可以通过
else
块环绕循环,或者使用
返回语句终止程序

编辑:另外,正如其他人指出的,您的while条件(
roll!=7&&roll!=point
永远不会满足,因为在此阶段roll仍然等于
点。您可以通过将
roll
的值重新设置为默认值(例如0)或使用do while循环来解决此问题

使用do while循环,循环体将始终在检查条件之前执行一次

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!");
          return; //terminate the main function

        }

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

        }

        //do-while loop: execute and then check for condition
        //and then if condition holds, execute a second time and so on
        do {
            int d3 = (int) (6 * Math.random() + 1);
            int d4 = (int) (6 * Math.random() + 1);
            roll = d3 + d4;

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

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

另一方面,您似乎总是在while循环中打印相同的内容,你的意思是打印
roll
而不是
point

检查你的花括号show我会执行do-while循环吗?执行代码后,它仍然只打印一次点,而不是继续滚动,直到它再次到达点并打印出所述卷。