📜  Java程序将整数转换为布尔值

📅  最后修改于: 2022-05-13 01:54:41.184000             🧑  作者: Mango

Java程序将整数转换为布尔值

给定一个整数值,任务是将此整数值转换为Java中的布尔值。

例子:

Input: int = 1
Output: true

Input: int = 0
Output: false

方法:

  • 获取要转换的布尔值。
  • 检查布尔值是真还是假
  • 如果整数值大于等于 1,则将布尔值设置为 true。
  • 否则,如果整数值大于 1,则将布尔值设置为 false。

示例 1:

// Java Program to convert integer to boolean
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // The integer value
        int intValue = 1;
  
        // The expected boolean value
        boolean boolValue;
  
        // Check if it's greater than equal to 1
        if (intValue >= 1) {
            boolValue = true;
        }
        else {
            boolValue = false;
        }
  
        // Print the expected integer value
        System.out.println(
            intValue
            + " after converting into boolean = "
            + boolValue);
    }
}
输出:
1 after converting into boolean = true

示例 2:

// Java Program to convert integer to boolean
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // The integer value
        int intValue = 0;
  
        // The expected boolean value
        boolean boolValue;
  
        // Check if it's greater than equal to 1
        if (intValue >= 1) {
            boolValue = true;
        }
        else {
            boolValue = false;
        }
  
        // Print the expected integer value
        System.out.println(
            intValue
            + " after converting into boolean = "
            + boolValue);
    }
}
输出:
0 after converting into boolean = false