第3章:使用运算符&&、||和^

来源:互联网 发布:ubuntu软件下载 编辑:程序博客网 时间:2024/05/16 14:36
/** * 使用运算符&&、||和^ * 提示用户输入一个整数: * 判断它是否能被5和6整除,是否能被5或6整除,以及是否能被5或6整除但不能被它们整除。 */package Test;import java.util.Scanner;public class T326Scanner {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter an integer: ");int num = input.nextInt();boolean even = ((num % 5 == 0 && num % 6 == 0) ^ (num % 5 == 0 || num % 6 == 0));System.out.println((even == false)? "Is " + num + " divisible by 5 and 6? " + even : "\nIs " + num + " divisible by 5 or 6? " + even  + "\nIs " + num + " divisible by 5 or 6, but not both? " + even);}}

原创粉丝点击