不使用 if/else 语句的布尔语句?

发布于 2025-01-15 16:07:14 字数 737 浏览 1 评论 0原文

需要打印一个将返回的布尔语句 - 如果输入数字在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

中性美 2025-01-22 16:07:14

第二个“最大”条件只需适用于 40 到 50 范围内的数字。否则,奇数/偶数并不重要。考虑这个版本:

if (value > 0 && value < 40 ||
    value >= 40 && value <= 50 && value % 2 == 1 ||
    value > 50 && value < 101) {

    System.out.println(valuetest);
}

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:

if (value > 0 && value < 40 ||
    value >= 40 && value <= 50 && value % 2 == 1 ||
    value > 50 && value < 101) {

    System.out.println(valuetest);
}
雄赳赳气昂昂 2025-01-22 16:07:14

在不同的函数中编写将返回 true/false 的代码,如下所示:

public bool ResultVal(int value)
{
   if(value >=1 && value <= 100)
   {
     if(value >=40 && value <=50)
     {
        if(value % 2 == 0)
        {
          return false;
        } 
     }
     return true; 
   }
   return false;
}

并从 main 方法中调用该函数:

public static void main(String[] args) 
{

   int value;

    System.out.println("enter number");
    Scanner scan = new Scanner(System.in);
    value = scan.nextInt();
    System.out.println("Result: " + ResultVal(value));
    scan.close();
}

Write code in a different function that will return true/false as below:

public bool ResultVal(int value)
{
   if(value >=1 && value <= 100)
   {
     if(value >=40 && value <=50)
     {
        if(value % 2 == 0)
        {
          return false;
        } 
     }
     return true; 
   }
   return false;
}

and call the function from your main method:

public static void main(String[] args) 
{

   int value;

    System.out.println("enter number");
    Scanner scan = new Scanner(System.in);
    value = scan.nextInt();
    System.out.println("Result: " + ResultVal(value));
    scan.close();
}
左岸枫 2025-01-22 16:07:14

由于“假”列表中的值很少:

boolean valuetest = value >= 1 && value <= 100 
    && !List.of(40, 42, 44, 46, 48, 50).contains(value);

可读性很重要。

`

Since there are so few values in the "false" list:

boolean valuetest = value >= 1 && value <= 100 
    && !List.of(40, 42, 44, 46, 48, 50).contains(value);

Readability counts.

`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文