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

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

数字%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]);
		}
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值