خرید بک لینک

Vote count: 0

I have the following simple recursive Fibonacci code:

public class FibPrac5202016
{
public static void main(String [] args)  {
    Scaer input = new Scaer(System.in);
    System.out.println("Enter index number: ");
    int integer = input.nextInt();
  FibPrac5202016 object = new FibPrac5202016();

System.out.println(object.operation(integer));
}

public static long operation(long n) {
 if(n==0)
     retu 0;
 if(n==1)
     retu 1;
 try {
     if( n < 0)
     throw new Exception("Positive Number Required");

 }
 catch(Exception exc)
 {
     System.out.println("Error: " + exc.getMessage());

 }

 retu operation((n-1))+operation((n-2));
}

}

As I recently leaed about exceptions, I'm trying to use that here when the user inputs negative integer.However, my program runs into StackOverflowError.

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 1 خرداد 1395 ساعت: 10:59

صفحه بندی