水仙花数-java实现

来源:互联网 发布:恒生直销软件 编辑:程序博客网 时间:2024/05/17 22:32

                                 基础题,用带有一个输入参数的函数或方法实现,返回值类型为布尔类型。

代码:

package shuixianhua;

import java.util.Scanner;

public class Shuixian {
//@SuppressWarnings({ "unused", "resource" })
public static void main(String arg[]){
Scanner sin = new Scanner(System.in);
System.out.println("请输入一个数:" );
int num = sin.nextInt();
int a,b,c;
a = num / 100;   //百位数;
b = num / 10 % 10;     //十位数;
c = num %10;         //个位数;
if(num  >=100  &&  num <= 999){
if(num == a * a * a +  b * b * b + c * c * c ){
System.out.println(num + "是水仙花数");
}else{
System.out.println(num + "不是水仙花数");
}
}else{
System.out.println("你的输入有误");
}
}
}

运行结果;

请输入一个数:
153
153是水仙花数

45
你的输入有误

345
345不是水仙花数


0 0