编写一个Java程序,使用 If Else 语句和条件运算符检查数字是否可以被 5 和 11 整除,并附带示例。
Java程序检查数字是否可以被 5 和 11 整除 示例 1
此Java程序帮助用户输入任何数字。接下来,它使用If Else 语句检查给定数字是否同时可以被 5 和 11 整除。
// Java Program to Check whether Number is Divisible by 5 and 11
import java.util.Scanner;
public class Divisibleby5and11 {
private static Scanner sc;
public static void main(String[] args)
{
int number;
sc = new Scanner(System.in);
System.out.print(" Please Enter any Number to Check whether it is Divisible by 5 and 11 : ");
number = sc.nextInt();
if((number % 5 == 0) && (number % 11 == 0))
{
System.out.println("\n Given number " + number + " is Divisible by 5 and 11");
}
else
{
System.out.println("\n Given number " + number + " is Not Divisible by 5 and 11");
}
}
}

让我尝试在Java示例中尝试另一个值。
Please Enter any Number to Check whether it is Divisible by 5 and 11 : 205
Given number 205 is Not Divisible by 5 and 11
Java程序使用条件运算符验证数字是否可以被 5 和 11 整除
此程序使用三元运算符来检查给定数字是否同时可以被 5 和 11 整除。
// Java Program to Check whether Number is Divisible by 5 and 11
import java.util.Scanner;
public class Divisibleby5and11Ex2 {
private static Scanner sc;
public static void main(String[] args)
{
int number;
sc = new Scanner(System.in);
System.out.print(" Please Enter any Number to Check whether it is Divisible by 5 and 11 : ");
number = sc.nextInt();
String message = ((number % 5 == 0) && (number % 11 == 0))? " is Divisible by 5 and 11": " is Not Divisible by 5 and 11";
System.out.println("\n Given number " + number + message);
}
}
Please Enter any Number to Check whether it is Divisible by 5 and 11 : 55
Given number 55 is Divisible by 5 and 11