A positive integer is called a “self-divisor” if every decimal digit of the number is a divisor of the number, that is, the number is evenly divisible by each and every one of its digits. For example, the number 128 is a self- divisor because it is evenly divisible by 1, 2, and 8. However, 26 is not a self-divisor because it is not evenly divisible by the digit 6. Note that 0 is not considered to be a divisor of any number, so any number containing a 0 digit is NOT a self-divisor. There are infinitely many self-divisors.

Finish writing method isSelfDivisor below, which takes a positive integer as its parameter. This method returns true if the number is a self-divisor; otherwise, it returns false. The main method includes tests to check if this method is working correctly.

```public class SelfDivisor
{

/** @param number the number to be tested
*         Precondition: number > 0
*  @return true if every decimal digit of
*          number is a divisor of number;
*          false otherwise
*/
public static boolean isSelfDivisor(int number)
{
// part A
}

/****************/

public static void main (String[] args)
{
System.out.println("128: " + isSelfDivisor(128));
System.out.println("26: " + isSelfDivisor(26));
System.out.println("120: " + isSelfDivisor(120));
System.out.println("102: " + isSelfDivisor(102));
}
}```

## 如何解题

```public class Test
{
public static void main(String[] args)
{
System.out.println(128 % 8);
System.out.println(128 % 2);
System.out.println(128 % 1);
}
}```

```public class Test
{
public static void main(String[] args)
{
System.out.println(26 % 6);
}
}```

```public class Test
{
public static void main(String[] args)
{
System.out.println(128 % 10);
System.out.println(128 / 10);
System.out.println(12 % 10);
System.out.println(12 / 10);
}
}```

0:00

Which loop should you use to loop through all the digits of the number?

3

What should you use as the test in the while loop?

3

```public static boolean isSelfDivisor(int number) {
int n = number;
while (n > 0) {
int digit = n % 10;
if (digit == 0 || number % digit != 0) {
return false;
}
n /= 10;
}
return true;
}```