public static void main(String[] args) { /** * 示例: * * 输入:19 * 输出:true * 解释: * 1^2 + 9^2 = 82 * 8^2 + 2^2 = 68 * 6^2 + 8^2 = 100 * 1^2 + 0^2 + 0^2 = 1 * * #思路 */ Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); Set<Integer> set = new HashSet<>(); while (!set.contains(num)){ set.add(num); num = getNum(num); } System.out.println(num==1); } private static int getNum(int num) { int res = 0; while (num > 0){ int k = num % 10; res += k * k; num = num / 10; } return res; }
总结:对于一个数如何通过while循环获取他的个位、十位、百位的数字。
标签:set,scanner,判断,int,res,快乐,是不是,while,num From: https://www.cnblogs.com/sgj191024/p/17557805.html