Java程序检查数字的所有位数是否都能整除它


要检查数字的所有位数是否都能整除它,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("All the digits of the number divide the number completely.");
      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”的函数,该函数有两个参数:数字和位数。此函数根据返回的输出是真还是假返回布尔值。它检查数字是否不为0,以及数字除以数字的位数是否能完全整除。

另一个名为“divide_digits”的函数是一个布尔函数,它以数字作为参数。此函数检查数字中的所有位数是否都能完全整除该数字。在主函数中,定义了数字的值并使用此值调用该函数,如果它返回“true”,则显示相关消息。如果不是,则会显示一条消息,说明该数字不能被完全整除。

更新于:2020年7月8日

219 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告