Java基礎——使用三元運算符判斷一個數的奇偶性
阿新 • • 發佈:2017-06-03
print sta stat ont strong scan ron -s color
要求:
使用三元運算符判斷一個數的奇偶性
實現代碼:
/** * 使用三元運算符判斷用戶輸入的一個數的奇偶性 */ import java.util.Scanner; public class Odd_even { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("請輸入一個整數:"); long num1 = input.nextLong(); //三元運算符:表達式1?表達式2:表達式3;表達式1結果為true則返回表達式2,為false則返回表達式3String str = (num1%2==0)? "這個數是2的偶數!":"這個數是奇數!"; System.out.println(str); } }
運行結果:
請輸入一個整數: 46545 這個數是奇數!
Java基礎——使用三元運算符判斷一個數的奇偶性