要检查数字的所有数字是否将其除,Java代码如下:
示例import java.io.*;
public class Demo{
static boolean divisibility_check(int val, int digit){
return (digit != 0 && val % digit == 0);
}
static boolean divide_digits(int val){
int temp = val;
while (temp > 0){
int digit = val % 10;
if ((divisibility_check(val, digit)) == false)
return false;
temp /= 10;
}
return true;
}
public static void main(String args[]){
int val = 150;
if (divide_digits(val))
System.out.println("数字的所有数字将数字完全除。");
else
System.out.println("All the digits of the number are not divided by the number
completely.");
}
}
输出结果All the digits of the number are not divided by the number completely.
名为Demo的类包含一个名为“ divisibility_check”的函数,该函数具有两个参数-数字和数字。此函数根据返回的输出是true还是false来返回布尔值。它检查数字是否不为0,以及数字除以数字的位数是否被完全除。
另一个名为“ divide_digits”的函数是一个布尔函数,它以数字作为参数。此功能检查数字中的所有数字是否将数字完全除。在主函数中,定义了一个数字值,并使用该值调用该函数,如果返回“ true”,则显示相关消息。如果不是,那么它会显示一条消息,指出数字不能被完全除。