我编写了这段代码来找出输出,我正在使用运行时在 servlet 中运行它。即使我检查了输入文件有一些数据,它也会显示 java.util.NoSuchElementEception:
public class Sec1q10 {
static int fact(int n) {
int p = 1;
if (n != 1) {
p = n * fact(n - 1);
}
return p;
}
public static void main(String args[]) {
try {
System.out.println("first");
Scanner in = new Scanner(new FileReader("F:/sem5/algorithm/in.txt"));
String no = in.next();
int n = Integer.parseInt(no);
System.out.println(n);
int s = 0;
while (n != 0) {
s += fact(n);
n--;
}
System.out.println("sum=" + s);
String s1 = "" + s + "here";
PrintWriter out;
System.out.println(s1);
out = new PrintWriter("F:/sem5/algorithm/out.txt");
out.write(s1);
System.out.println(s1);
} catch (Exception ex) {
System.out.println("Exception: " + ex);
}
}
}
我什至在 cmd 上运行它,它毫无例外地显示输出,但没有在文件 F:/sem5/algorithm/out.txt 中写入任何内容