Commit 86b1a070 authored by uievq's avatar uievq Committed by uxrog
Browse files

added a exit possibility to endless loop in Factorial

parent 125c57a6
0
1
2
3
4
5
6
7
8
0!
1!
2!
3!
4!
5!
6!
7!
8!
9
......@@ -6,3 +6,4 @@
720
5040
40320
-1
......@@ -3,8 +3,13 @@ class Factorial {
Calc c = new Calc();
c.reset();
while (true) {
c.getInput();
c = new Calc();
int result = c.getInput();
if (result == -1) {
System.out.println(-1);
return;
}
System.out.println(result);
c = new Calc();
c.reset();
}
}
......@@ -22,19 +27,18 @@ class Calc {
number = 0;
}
public void getInput() throws Exception {
public int getInput() throws Exception {
readNext();
if (input == 10) readNext();
while (isDigit(input)) {
number = 10 * number;
number = number + (input - 48);
readNext();
}
if (input == 33) {
System.out.println(fact(number));
} else if (input != 10) {
System.out.write(33);
System.out.write(10);
System.out.flush();
return fact(number);
} else {
return -1;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment