代码主体:
//水仙花数的判断
public static void main(String[] args) {
//定义
int a, b, c, d;
System.out.println("输入要判断的数");
//抓取命令
Scanner scanner = new Scanner(System.in);
d = scanner.nextInt();
//拆数
a = d % 10;//个位
b = d / 10 % 10;//十位
c = d / 100;//百位
//用分支来判断水仙花数
if (a * a * a + b * b * b + c * c * c == d) {
System.out.println("是水仙花数");
} else System.out.println("不是水仙花数");
}
}
输出结果:
输入要判断的数
652145
不是水仙花数
Process finished with exit code 0