package myproject;
/**
*
* @author 李瑞琦
* 随机生成一个三位数,判断这个三位数是否是水仙花数
*
*/
public class TestNarcissisticNumber {
public static void main(String[] args) {
int number = (int)(Math.random()*900+100);
boolean flag = testNumber(number);
if(flag){
System.out.println("您输入的"+number+"是水仙花数");
}else{
System.out.println("您输入的"+number+"不是水仙花数");
}
}
public static boolean testNumber(int number){
int x = number/100;
int y = number/10%10;
int z = number%10;
int total = x*x*x+y*y*y+z*z*z;
if(total==number){
return true;
}else{
return false;
}
}
}
随机生成一个三位数判断是否为水仙花数
最新推荐文章于 2024-10-07 12:08:06 发布