Java 当循环不退出时
在这方面,Java还是个新手。我试着运行一个程序,掷两个骰子,当两个骰子之间的总掷骰数为7或11时,程序将退出。由于某种原因,我的while语句一遍又一遍地执行,我不知道为什么。任何帮助都将不胜感激。代码如下Java 当循环不退出时,java,while-loop,Java,While Loop,在这方面,Java还是个新手。我试着运行一个程序,掷两个骰子,当两个骰子之间的总掷骰数为7或11时,程序将退出。由于某种原因,我的while语句一遍又一遍地执行,我不知道为什么。任何帮助都将不胜感激。代码如下 if ((totalRoll == 7.0) || (totalRoll == 11.0)) { System.out.println("You win!"); break; } 再一次,这是非常新的。目前并没有太多的关注于一种更有效的方法
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
再一次,这是非常新的。目前并没有太多的关注于一种更有效的方法来实现这一点,而是关注为什么这一功能无法发挥作用的语义细节
// 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
*/
}
}
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
我想你想把你的
或
换成和
。对于或,while
语句的值将始终为true,因为单个数字不可能同时是7
和11
(除非是一些残酷的量子计算,但我假设不是)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
注意:如果你想做一个量子骰子,那么有人可能会说你需要无限次地掷骰子,因为单个di面可以同时等于所有数字,所以在这种情况下,你的程序运行得很好。。。值得思考
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
严肃注意事项:使用双精度
表示骰子上的值确实是浪费。你保留的内存比你需要的多得多。如果要优化内存使用,请使用int
甚至字节
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
根据评论进行编辑
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
您将中断添加到if
语句中;我想是吧。嗯,你看,这是不同的。如果这个数字等于这些数字中的一个,那么你就是在声称。让我给你举个例子
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
在处理平等问题时
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
if语句中的逻辑很好,因为:
double totalRoll = 7.0;
if(totalRoll == 7.0 || totalRoll == 11.0)
{
// Do something.
}
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
其结果是:
if(7.0 == 7.0 || 7.0 == 11.0)
{
}
if(false)
while(false || true)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
即:
if(true || false)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
因为TRUE或X==TRUE
,它变成:
if(true)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
同样,如果将totalRoll
设置为9.0
:
if(9.0 == 7.0 || 9.0 == 11.0)
{
}
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
这就变成了
if(false || false)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
最终评估为:
if(7.0 == 7.0 || 7.0 == 11.0)
{
}
if(false)
while(false || true)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
所以我们已经通过演示证明,这个if语句
可以处理输入。让我们试试你的while
循环
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
在处理不平等问题时
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
评估为
while(7.0 != 7.0 || 7.0 != 11.0)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
评估结果如下:
if(7.0 == 7.0 || 7.0 == 11.0)
{
}
if(false)
while(false || true)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
再一次,X或TRUE==TRUE
so
while(true)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
哦,天哪。因此,正如您所看到的,这个while
循环将中断的唯一方法是totalRoll
的值同时为7.0
和11.0
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
为什么解决方案有效
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
我们通过将或
替换为和
来修复它
double totalRoll = 7.0;
while(7.0 != 7.0 && 7.0 != 11.0)
{
}
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
评估为
while(false && true)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
我们知道,FALSE和X==FALSE
so
while(false)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
循环中断了。同样的情况也会发生在11.0上,但不会发生在任何其他数字上。您可以在System.out.print之后添加中断
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
您可能希望while
语句是这样的——它更清晰:
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
while (!(totalRoll == 7.0 || totalRoll == 11.0))
“虽然掷骰等于7或11是不正确的……”像这样比较双打是很有问题的;由于双精度值的表示方式,totalRoll==7.0
不会为您提供真值,即使totalRoll
正好为7.0。比较双精度的更好方法(如果必须使用双精度)如下:
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
while (Math.abs(totalRoll - 7.0) > 0.0001 && Math.abs(totalRoll - 11.0) > 0.0001) { ... }
这样,保存双重值的不准确不会对您造成太大的伤害
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
编辑:由于有反对票,我将发布更多信息来支持我的观点:
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
- 是一篇很好的文章,展示了与浮点和双精度相关的各种问题
- 详细说明如何安全地比较两个双精度值
- 是Java标准库中的一个类,适用于任何精度,因此其函数将比上面解释的方法更精确
此外,以下代码为我打印false
:
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
System.out.println(((7.0 / 100) * 100) == 7.0);
如果有人有信息反驳我的话,请告诉我。Re:@Paranix“&是按位的”
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
以下是苏的一句话:
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
“和&&and | |运算符‘短路’,这意味着如果没有必要,它们不会计算右侧。和|运算符用作逻辑运算符时,总是计算两侧。”
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
我发现这和31张赞成票,在其他地方也发现了类似的措辞。但可能那个引用需要修改,因为&确实是“按位和”,句号
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
也就是说,如果两个操作数恰好都是布尔数,它们就是整数,可以是1或0,所以按位填充适用
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
1&1为真(等于1);1&0、0&1、0&0为假(等于0)
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
下面的语句打印0,0,0,1:
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
System.out.println((1&0) + "," + (0&1) + "," + (0&0) + "," + (1&1));
如果使用(1&&0),则会出现错误,因为&&需要布尔操作数
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
在任何情况下,修改后的骰子代码都可以与&或&&一起正确工作
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}
我已经读到,如果右操作数有可能为空,则肯定需要&,但我无法证明这一点。什么是(totalRoll!=7.0)|(totalRoll!=11.0)
的意思?一个数字可以等于两个值吗?如果总掷骰数不是7或11,则重新掷骰子直到命中7或11,为什么要使用double
s来表示骰子?想想你希望它们能有哪些值。我知道它们是整数,但表示.0并不真正影响程序的功能,是吗?@Zack,如果滚动7,而时仍会继续,因为它也不等于11totalRoll==7.0
但totalRoll!=11.0
。这满足条件,因为您使用的是或.True,但这并不能真正解释while
循环没有按OP预期的方式工作。但是当我添加中断时,while循环似乎工作正常。手术室似乎明白了我想做什么?如果一个数字不是这个或不是这个,那么重新滚动。我刚刚添加了一个大的ol演示为什么while循环不起作用。这对理解
if ((totalRoll == 7.0) || (totalRoll == 11.0))
{
System.out.println("You win!");
break;
}