看下我的把 很简单的 :
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int x=0;
try
{
i=in.nextInt();
}
catch(Exception e)
{
System.out.println("输入非数字!");
return;
}
if(x<100||x>=1000)
{
System.out.println("输入的不是三位数!");
return;
}
if(isSX(x))
{
System.out.println(x"是水仙花数!");
}
else
{
System.out.println(x"不是水仙花数!");
}
}
static boolean isSX(int x)
{
String s="";
s=s+x;
int i1=new Integer(s[0]);
int i2=new Integer(s[1]);
int i3=new Integer(s[2]);
if((i1+i2+i3)==x)
{
return true;
}
else
{
return false;
}
}
}
public class WaterFlower
{
/**
*下面这个方法就是楼主需要的
*/
public static void f1(int i)
{
String s=(new Integer(i)).toString();
int i1=Integer.parseInt(s.substring(0,1));
int i2=Integer.parseInt(s.substring(1,2));
int i3=Integer.parseInt(s.substring(2));
if(i1*i1*i1+i2*i2*i2+i3*i3*i3==i)
{
System.out.println(i+"是水仙花数");
}
else
{
System.out.println(i+"不是水仙花数");
}
}
public static void main(String[] args)
{
f1(234);//往里传入要判断的数字进行调用
}
}
这题 我以前做过
用 while 循环取出每一位(j)
用字符串(nums) 连接起来
nums += j + "_";
在定义一个数组 来接受
int[] num=num.split('_'); //是一个字符串的方法 以'_' 为标志来分割字符串 返回的只数组
再来判断 数组中每个值的 三次方 是否等于 i
等于返回 True 否则返回 false