一列数的规则如下:1,1,2,3,5,求第三十位数java递归程序

2024-12-12 11:03:55
推荐回答(2个)
回答1:


import java.io.IOException;
import java.io.InputStream;

public class Digui {

/**
* @param args
*/
public static void main(String[] args) {
byte[] b = new byte[32];
while (true) {
try {
System.out.println("请输入正整数,获取其阶乘,输入over结束");
System.in.read(b);
String res = new String(b).trim();
if (res.equals("over")) {
System.out.println("程序结束");
break;
}
int i = Integer.parseInt(res.trim());
if (i <= 0)
continue;
System.out.println(i + "!=" + getResult(i));
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException ne) {
System.out.println("请输入正整数");
}
}
}

public static int getResult(int i) {
return i == 1 ? i : i * getResult(i - 1);
}
}

回答2:

1,1,2,3。5,8,13,21,34,55,89,144,233