java判断一个三位数字是否水仙花数

来源:互联网 发布:淘宝优木家具怎么样 编辑:程序博客网 时间:2024/06/05 16:58


/**输入一个三位数判断是不是水仙花数 水仙花数是指一个三位数的各位的立方和等于该数本身。 */import java.util.Scanner;public class TestWork {public static void main(String[] args) {System.out.println("请输入一个三位数");Scanner sc = new Scanner(System.in);if (sc.hasNextInt()) {int num = sc.nextInt();if (num > 99 && num < 1000) {int a = num / 100;// 百int b = num / 10 % 10;// 十int c = num % 10;// 个int result = (int) Math.pow(a, 3) + (int) Math.pow(b, 3) + (int) Math.pow(c, 3);if (result == num) {System.out.println(num + "是水仙花数");} else {System.out.println(num + "不是水仙花数");}} else {System.out.println("请输入一个三位数");}} else {System.out.println("请输入正整数");}}}

/**找出100-999之间所有的水仙花数 */public class TestWork {public static void main(String[] args) {int Narcount=0;//计数for (int i = 100; i <= 999; i++) {int a = i / 100;// 百int b = i / 10 % 10;// 十int c = i % 10;// 个int result = (int) Math.pow(a, 3) + (int) Math.pow(b, 3) + (int) Math.pow(c, 3);if (result == i) {Narcount++;System.out.print(i+"\t");                  if(Narcount%5==0){                      System.out.println();                  }  } }}}