public class fakultaet1 {
public static long fakultaet(long n) {
if (n<0)
throw new FakultaetNichtDefiniertException(n);
if (n==0)
return 1;
long fakultaet = 1;
while(n>1){
fakultaet *= n; // had a litte mistake here
n--;
}
return fakultaet;
}
public class FakultaetNichtDefiniertException extends RuntimeException{
public FakultaetNichtDefiniertException(long n){
super("Die Fakultät is für den Wert "+ n +" nicht definiert.");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(fakultaet(5));
}
}
So i want my code to calculate the factorial of the input n, and it should throw an Exception when the number is less than 0, but if i try to run it gives me this output.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type fakultaet1 is accessible. Must qualify the
allocation with an enclosing instance of type fakultaet1 (e.g. x.new A()
where x is an instance of fakultaet1).
at klausur_ws1718.fakultaet1.fakultaet(fakultaet1.java:8)
at klausur_ws1718.fakultaet1.main(fakultaet1.java:29)
I do not really understand the Error. Thanks in advance.