for循环练习题1——水仙花数

/*
输出所有的水仙花数,
所谓水仙花数是指一个3位数,
其各个位上数 字立方和等于其本身。
例如: 153 = 1*1*1 + 3*3*3 + 5*5*5

*/
class ForTest3{

  public static void main(String[] args){
    int num1 = 0;//百位
    int num2 = 0;//十位
    int num3 = 0;//个位
    for(int i = 100; i < 1000; i++){
      num1 = i/100;//获取三位数中的百位数字
      num2 = i%100/10;//获取三位数中的十位数字
      num3 = i%100%10;//获取三位数中的个位数字
      //获取三次方
      double c1 = Math.pow(num1,3);
      double c2 = Math.pow(num2,3);
      double c3 = Math.pow(num3,3);
      //判断
      if((c1+c2+c3) == i){
      System.out.println(i);
      }
    }
  }
}

原文地址:https://www.cnblogs.com/zhou-x/p/10833474.html