不使用 if/else 语句的布尔语句?
需要打印一个将返回的布尔语句 - 如果输入数字在 1 到 100 范围内,则为“true”;如果输入数字为 40 到 50 之间的偶数,则为“false”。
- 例如,如果您输入 12、45、60 等...您应该得到 true。
- 如果您输入 42、44 或 48,您应该得到 false。
这是我到目前为止的代码, 我陷入困境,因为如果我输入低于 40(例如 12)或高于 50(例如 60)的数字,它会打印“false”。这是我唯一需要帮助修复的部分。在这里编程的初学者,因此将不胜感激任何帮助。
public static void main(String[] args) {
int value;
System.out.println("enter number");
Scanner scan = new Scanner(System.in);
value = scan.nextInt();
scan.close();
boolean valuetest, min, max;
min = (value>0 && value <101);
max = (value >=40 && value <= 50 && value%2 ==1);
valuetest= (max && min);
System.out.println( valuetest);
Need to print a boolean statement that will return
-'true' if input number is within a range of 1 to 100 but 'false' if it's an even number between 40 and 50.
- e.g if you enter 12,45,60,etc...you should get true.
- if you enter 42 or 44 or 48 you should get false.
Here's the code I have so far,
I'm stuck because it prints 'false' if I enter a number that's below 40, e.g 12 or above 50, e.g 60. This is the only part I need help fixing. Beginner in programming here so would appreciate any help.
public static void main(String[] args) {
int value;
System.out.println("enter number");
Scanner scan = new Scanner(System.in);
value = scan.nextInt();
scan.close();
boolean valuetest, min, max;
min = (value>0 && value <101);
max = (value >=40 && value <= 50 && value%2 ==1);
valuetest= (max && min);
System.out.println( valuetest);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第二个“最大”条件只需适用于 40 到 50 范围内的数字。否则,奇数/偶数并不重要。考虑这个版本:
Your second "max" condition only needs to apply to numbers falling into the range of 40 to 50. Otherwise, odd/even does not matter. Consider this version:
在不同的函数中编写将返回 true/false 的代码,如下所示:
并从 main 方法中调用该函数:
Write code in a different function that will return true/false as below:
and call the function from your main method:
由于“假”列表中的值很少:
可读性很重要。
`
Since there are so few values in the "false" list:
Readability counts.
`