100到999的水仙花數java實現
public class shuixianhuashu {
public static void main(String[] args) { // TODO Auto-generated method stub int cout=0; for(int i=1;i<=999;i++){ if(isshuixianhua(i)){ cout++; System.out.printf(" %d",i); if(0 == cout%10) System.out.println(); } } System.out.printf("\n在100到999中屬於水仙數的一共有: %d個", cout); } public static boolean isshuixianhua(int number){ if((number<100)||(number>999)) { return false; } int gewei=(number%10)%10; int shiwei=(number/10)%10; int baiwei=(number/100); if(number==(Math.pow(gewei, 3)+Math.pow(shiwei, 3)+Math.pow(baiwei, 3))){ return true; }else { return false; } } }