//下面是我的代码,不足的地方希望给点建议,
public enum BeautyNums {
//水仙花
Daffodil(3),
//班花数
ClassFlower(4),
//级花数
GradeFlower(5),
//校花数
Oh(6);
private Map<Integer,Double> nums = new HashMap<Integer,Double>();
//参数代表1-9的几次方
BeautyNums(int num){
for(int i=0;i<9;i++){
nums.put(i, Math.pow(i,num)); //将个花的计算(1-9)的次方一次存放在map集合中
}
}
public Double getNum(int num){
return this.nums.get(num);
}
}
public class NumTest {
public static void main(String[] args) throws IOException {
NumTest nt = new NumTest();
System.out.println("请输入正整数N(100<N<1000000)");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int [] nums; //存储输入数字
int count=0; //统计输入的数字数量
String line;
while(!("over".equals(line=br.readLine()))){
sb.append(line+" ");
count++;
}
nums = new int[count];
String [] ns = sb.toString().split(" ");
for(int i =0;i<ns.length;i++){
nums[i]=Integer.valueOf(ns[i]);
if(nums[i]<100 ||nums[i]>1000000)
throw new RuntimeException("您输入的数字已超出范围");
System.out.println(nt.calName(nums[i]));;
}
}
//计算num的类型
public String calName(int num){
String numName = "numName: ";
char [] c ={'b','q','w','s'}; //'b'--3位数 'q'--四位数 'w'--代表五位数 's'--代表六位数
//将整数换成字符串判断几位
String len = String.valueOf(num);
char flag =c[len.length()-3]; //判断该整数对应c[]数组中的字符
double sum=0;
switch(flag){
case 'b':
for(int i=0;i<len.length();i++){
int index=Character.getNumericValue(len.charAt(i));
sum+=BeautyNums.Daffodil.getNum(index);
}
if(sum==num){
numName+="D";
}else{
numName+="N";
}
break;
case 'q':
for(int i=0;i<len.length();i++){
sum+=BeautyNums.ClassFlower.getNum(Character.getNumericValue(len.charAt(i)));
}
if(sum==num){
numName+="C";
}else{
numName+="N";
}
break;
case 'w':
for(int i=0;i<len.length();i++){
sum+=BeautyNums.GradeFlower.getNum(Character.getNumericValue(len.charAt(i)));
}
if(sum==num){
numName+="G";
}else{
numName+="N";
}
break;
case 's':
for(int i=0;i<len.length();i++){
sum+=BeautyNums.Oh.getNum(Character.getNumericValue(len.charAt(i)));
}
if(sum==num){
numName+="O";
}else{
numName+="N";
}
break;
default:
numName+="N";
break;
}
return numName;
}
}
|