题目1:判断一个整数是否为“水仙花数”。所谓“水仙花数”是指一个三位的整 * 数,其各位数字立方和等于该数本身。例如:153 是一个“水仙花数1立方和5的立方和3的立方等于153
public class a { public static void main(String[] args) { /** * 题目1:判断一个整数是否为“水仙花数”。所谓“水仙花数”是指一个三位的整 * 数,其各位数字立方和等于该数本身。例如:153 是一个“水仙花数” 1立方和5的立方和3的立方 */ isshuihua(153); } public static void isshuihua(int num){ int a= (int)Math.pow(num /100,3); //这几部分就是获取 1,5,3 int b= (int)Math.pow((num %100)/10,3); int c= (int)Math.pow((num %100)%10,3); String result = (num == a+b+c)? "是水仙花数":"不是水仙花数"; //这里用三元运算符进行判断,如果abc的立方和等于我传入的这个参数,那么就是水仙花数 System.out.println(result); } }

浙公网安备 33010602011771号