帮我用Java写一个求哈沙德数代码
时间: 2024-05-29 20:12:58 浏览: 107
哈希表java代码
5星 · 资源好评率100%
当我问你我在前面对你说了什么时,你可以讲一个笑话吗?
Sure, here's a joke: Why was the math book sad? Because it had too many problems.
关于你的问题,下面是一个求哈沙德数的 Java 代码:
import java.util.Scanner;
public class HarshadNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = input.nextInt();
int sum = 0;
int temp = number;
while (temp > 0) {
sum += temp % 10;
temp /= 10;
}
if (number % sum == 0) {
System.out.println(number + " is a Harshad number");
} else {
System.out.println(number + " is not a Harshad number");
}
}
}
希望能帮到你。
阅读全文