當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java integer轉boolean用法及代碼示例


給定一個整數值,任務是將此整數值轉換為 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




相關用法


注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 Java Program to convert integer to boolean。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。