Ternary operator is odd or even is determined (Java programming classical case)

Achieved by determining the number of parity ternary operator, the program requires the user to enter an integer, then the program determines is odd or even, and outputs to the console. code show as below:

import java.util.Scanner;
/**
 * 用三元运算符判断是奇数还是偶数
 */
public class Example {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); //创建输入流扫描器
        System.out.println("请输入一个整数:");
        //使用扫描器的nextLong()方法直接获取了整型数据,避免了类型转换等业务代码
        Long number = scanner.nextLong(); //获取用户输入的整数
        String check = (number % 2 == 0)? "这个数是偶数":"这个数是奇数";
        System.out.println(check);
    }
}

Execution results as shown below:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/cui_yonghua/article/details/91976135