StackOverflowError

import java.util.Scanner;
public class Main {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc=new Scanner(System.in);
  int n= sc.nextInt();
  long sum=sum(n);
  System.out.println(sum);
 }
 public static long sum(int n) {
  if(n>0) {
   return n+sum(n-1);
  }else {
   return 0;
  }
 }
}
当输入n值过大时,就出现了StackOverflowError错误,求解决

你可能感兴趣的:(StackOverflowError)