虽然循环没有退出

Zac*_*ack -1 java while-loop

这方面是Java新手.我正在尝试运行一个滚动两个骰子的程序,当两个骰子之间的总滚动为7或11时,程序将退出.出于某种原因,我的while语句一遍又一遍地执行,我无法弄清楚原因.任何帮助将非常感激.代码如下.

再次,这是非常新的.不是那么专注于一种更有效的方式来实现这一点,而是关注为什么这不起作用的语义细节.

// import required packages for the program
// import all classes to be on the safe side
// will be using input/ouput and math, so those imports are needed
import java.io.*;
import java.math.*;
import java.text.*;

// Declare a class, class name Dice chosen
public class Dice
{
// need to use throws IOException because we are dealing with input and output, simple main method will not suffice
    public static void main(String[] args) throws IOException
    {

        // declare variables for the two dice, randomize each
        // die can each be rolled for # between 1-6
        double dice1 = Math.round(Math.random() * 6) + 1;
        double dice2 = Math.round(Math.random() * 6) + 1;

        // declare variable for combined roll
        double totalRoll = dice1 + dice2;

        //create loop to determine if totalRoll is a 7 or 11, if not continue rolling
        while ((totalRoll != 7.0) || (totalRoll != 11.0))
            {
            dice1 = Math.round(Math.random() * 6) + 1;
            dice2 = Math.round(Math.random() * 6) + 1;

            totalRoll = dice1 + dice2;

            System.out.println("The roll on die 1 is: " + dice1);
            System.out.println("The roll on die 2 is: " + dice2);
            System.out.println("The total of the two rolls is: " + totalRoll + "\n");

            if ((totalRoll == 7.0) || (totalRoll == 11.0)) 
            {
            System.out.println("You win!");
            }
            }


        /*
        // declare in as a BufferedReader; used to gain input from the user
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));

        //Simulate tossing of two dice
        */




    }
}
Run Code Online (Sandbox Code Playgroud)

chr*_*her 5

我想你想交换你OR的AND.有OR,那while语句将始终评估为真,因为单号不能同时7并11在同一时间(除非它的一些残酷的量子计算,但我认为它不是).

while(totalRoll != 7.0 && totalRoll != 11.0) {
    // Continue.
}
Run Code Online (Sandbox Code Playgroud)

注意:如果你正在尝试制作一个量子骰子,那么可以说你需要无数次掷骰子,因为单个di face可以同时等于所有数字,所以在这种情况下你的程序运行得很漂亮......只是值得深思.

严重注意:使用adouble表示骰子上的值非常浪费.你需要的内存比你需要的多得多.如果您想优化内存使用,请使用int或甚byte至使用.

编辑以回应评论

你在你的if陈述中添加了休息时间; 我在假设.嗯,你看,这是不同的.您声称该数字是否等于其中一个数字.让我为你举个例子.

在处理平等问题时

if语句中的逻辑很好,因为,让我们说:

double totalRoll = 7.0;

if(totalRoll == 7.0 || totalRoll == 11.0)
{
    // Do something.
}
Run Code Online (Sandbox Code Playgroud)

这评估为:

if(7.0 == 7.0 || 7.0 == 11.0)
{
}
Run Code Online (Sandbox Code Playgroud)

哪个成了:

if(true || false)
Run Code Online (Sandbox Code Playgroud)

因为TRUE OR X == TRUE,它变成了:

if(true)
Run Code Online (Sandbox Code Playgroud)

同样,如果你设置totalRoll为9.0:

if(9.0 == 7.0 || 9.0 == 11.0)
{
}
Run Code Online (Sandbox Code Playgroud)

哪个成了.

if(false || false)
Run Code Online (Sandbox Code Playgroud)

最终评估为:

if(false)
Run Code Online (Sandbox Code Playgroud)

所以我们通过演示证明,这if statement可以处理输入.让我们试试你的while循环吧.

在处理不平等时

double totalRoll = 7.0;

// We expect this to stop the loop..
while(totalRoll != 7.0 || totalRoll != 11.0)
Run Code Online (Sandbox Code Playgroud)

评估到

while(7.0 != 7.0 || 7.0 != 11.0)
Run Code Online (Sandbox Code Playgroud)

这评估为:

while(false || true)
Run Code Online (Sandbox Code Playgroud)

再一次X OR TRUE == TRUE如此.

while(true)
Run Code Online (Sandbox Code Playgroud)

噢亲爱的.所以你可以看到,唯一的方法while循环将不会打破,如果是的价值totalRoll既7.0与11.0在同一时间.

为何解决方案有效

我们通过交换OR来修复它AND.

double totalRoll = 7.0;

while(7.0 != 7.0 && 7.0 != 11.0)
{
}
Run Code Online (Sandbox Code Playgroud)

评估到..

while(false && true)
Run Code Online (Sandbox Code Playgroud)

我们知道这FALSE AND X == FALSE一点

while(false)
Run Code Online (Sandbox Code Playgroud)

循环中断了.同样的情况会发生11.0,但不会与任何其他数字发生.