Java获取数字中单个数字,实例 获取水仙花数

来源:互联网 发布:mac液体唇膏三文鱼粉 编辑:程序博客网 时间:2024/05/17 20:29

获取整数中每一位数规律:

数字%10  

取余及就是个位数,2位数除10再取余,3位数除10再除10取余。。。。


package day1;public class shuixianshu {public static void main(String[] args) {boolean b = false;for (int i = 100; i < 1000; i++) {//b = shuiXian(i);b = shuixian(i);if (b) {System.out.println(i);}}System.out.println("-------------------");getAllShu(23456789);}// 第二种public static boolean shuiXian(int shu) {boolean flag = false;int t1 = shu / 100;int t2 = (shu % 100) / 10;int t3 = shu % 10; int sum=t1*t1*t1+t2*t2*t2+t3*t3*t3;//int sum = (int) (Math.pow(t1, 3) + Math.pow(t2, 3) + Math.pow(t3, 3));if (sum == shu) {flag = true;}return flag;}// 第一种public static boolean shuixian(int shu) {boolean flag = false;String tm = Integer.valueOf(shu).toString();char[] c_tm = tm.toCharArray();int tm1 = Integer.parseInt(Character.valueOf(c_tm[0]).toString());int tm2 = Integer.parseInt(Character.valueOf(c_tm[1]).toString());int tm3 = Integer.parseInt(Character.valueOf(c_tm[2]).toString());int sum = tm1 * tm1 * tm1 + tm2 * tm2 * tm2 + tm3 * tm3 * tm3;if (sum == shu) {flag = true;}return flag;}//获取每个数字public static void getAllShu(int shu){int[] m=new int[9];int index=0;//使用规律,小数取整只留整数部,及nm%10=mwhile(shu>0){m[index]=shu%10;index++;shu/=10;}for(int i=0; i<index; i++){System.out.println(m[i]);}}}


0 0
原创粉丝点击