[英]Is it possible to use a boolean expression in this manner?
我目前正在学习Java脚本并尝试新事物,例如,我希望查看是否可以将布尔表达式设置为结束(如果该表达式检测到从开始到结束的数字)。
或者换句话说,我想要的是3到8。我将明确说明我正在使用netbeans IDE。
在我的最后一个if语句中,我希望它结束询问过程。 我希望它是一个简单的修复程序,但是除非我创建了更多其他if语句,否则我想不出任何可以完成此操作的方法。
扫描仪输入=新的Scanner(System.in);
int[][] table;
boolean stopasking = true;
while (stopasking = true){
System.out.println("Let's make a magic Square! How big should it be? ");
int size = input.nextInt();
if (size < 0)
{
System.out.println("That would violate the laws of mathematics!");
System.out.println("");
}
else if (size >= 9)
{
System.out.println("That's huge! Please enter a number less than 9.");
System.out.println("");
}
else if (size <= 2)
{
System.out.println("That would violate the laws of mathematics!");
System.out.println("");
}
else if (size == 3)
{
stopasking = false;
}
}
您使用了assignmnent运算符=
应该使用==
代替
当size<0
时条件size<=2
成立,因此if
两个都可以使用一个
while(stopasking){
if (size <= 2) {
System.out.println("That would violate the laws of mathematics!\n");
} else if (size >= 9){
System.out.println("That's huge! Please enter a number less than 9.\n");
} else if (size == 3){
stopasking = false;
}
}
您可以通过这种方式使用布尔表达式作为退出循环的条件。 有人会说这是一个比break
更优雅的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.