JAVA题目,在线等急

已经是最高悬赏,求快求稳
2025-04-13 20:43:54
推荐回答(1个)
回答1:

直接给你写实现吧。包名啥的自己写。

第一个块写到main函数里

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
String str = null; 
System.out.println("请输入0-1000内的数字"); 
str = br.readLine();
if(isNumeric(str)){
    Int num0 = Integer.parseInt(str);
    if(num0<=0||num0=>1000){
        System.out.println("输入的不是0-1000内的数字!"); 
        return;
    }
    if(str.length!=3){
        System.out.println("用户输入的"+str+"不是水仙花数!"); 
        return;
    }
    char[] chars = str.toCharArray();
    Int num1 = Integer.parseInt(chars[0]);//获取百位数
    Int num2 = Integer.parseInt(chars[1]);//获取十位数
    Int num3 = Integer.parseInt(chars[2]);//获取个位数
    Int re = num1*num1*num1+num2*num2*num2+num3*num3*num3;
    if(re==num0){
        System.out.println("用户输入的"+str+"是水仙花数!"); 
        return;
    }else{
        System.out.println("用户输入的"+str+"不是水仙花数!"); 
        return;
    }
}else{
    System.out.println("请输入整数!"); 
}

第二块写到main函数外面,是个自己的方法

public static boolean isNumeric(String str){  
    Pattern pattern = Pattern.compile("[0-9]*");  
    return pattern.matcher(str).matches();     
}

没有考虑重复输入的问题,但是加了个判断是不是小数。自己取舍吧,反正是个实验,都是用的很简单的计算方法

没有跑过,自己跑跑看有啥问题。