java基础判断一个数是不是水仙花数


  public static void main(String[] args) {
        //首先输入一个三位数
        //先将百位 十位 个位 提出来  做他们的三次方  得到新的数
        //再将新的数 相加 是否得到它本身 是输出 不是输出不是

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个三位数");
        int num = scanner.nextInt();
        int a = num / 100;
        int b = num / 10 % 10;
        int c = num % 10;
        if (a * a * a + b * b * b + c * c * c == num) {
            System.out.println("他是水仙花数");
        } else {
            System.out.println("他不是水仙花数");
        }

    }
}


运行结果:

猜你喜欢

转载自blog.csdn.net/leizhenjiang/article/details/80382585