获取整数中每一位数规律:
数字%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=m
while(shu>0){
m[index]=shu%10;
index++;
shu/=10;
}
for(int i=0; i<index; i++){
System.out.println(m[i]);
}
}
}