java 递归实现水仙花数
时间: 2023-09-23 15:07:36 浏览: 94
Sure, Java递归实现水仙花数的代码如下:
```
import java.util.Scanner;
public class NarcissisticNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入数字n:");
int n = input.nextInt();
for (int i = 1; i <= n; i++) {
if (isNarcissistic(i)) {
System.out.println(i + "是水仙花数");
}
}
}
public static boolean isNarcissistic(int num) {
String str = String.valueOf(num);
int n = str.length();
int sum = 0;
for (int i = 0; i < n; i++) {
int digit = str.charAt(i) - '0';
sum += Math.pow(digit, n);
}
return sum == num;
}
}
```
请问你还有什么问题吗?
阅读全文