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


/**
输入一个三位数判断是不是水仙花数 水仙花数是指一个三位数的各位的立方和等于该数本身。
 */
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();  
                }  
			} 
		}
	}
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值