java 栈溢出问题求教

public static void print(int n) {   
    StringBuilder str=new StringBuilder();
    if(n<10) str=str.append(n);
    else {   		
    	he(n-9);
    	str=str.append(9);
    }
    System.out.print(str);
}
请问这个函数因为n的数字过大会导致栈溢出,报错Exception in thread "main" java.lang.StackOverflowError。
原因是每一次递归都会产生一个对象,请问这个该如何解决?
#Java#
全部评论
import java.util.Scanner; public class Main { public static void main(String[] args) {         Scanner in = new Scanner(System.in);         int n=in.nextInt();         int[] id=new int[n];         for(int i=0;i<n;i++)             id[i]=in.nextInt();         in.close();         for(int i=0;i<n;i++) {          StringBuilder str=new StringBuilder();         int tmp=id[i];         while(tmp>9) {          str.append(9);          tmp-=9;                }         System.out.print(tmp);         if(id[i]>9)System.out.print(str);         }                    } } 输入:1 100000 输出为空白...
点赞 回复 分享
发布于 2019-09-21 22:54
网易笔试?
点赞 回复 分享
发布于 2019-09-21 18:27

相关推荐

不愿透露姓名的神秘牛友
07-09 16:15
我应届生,去年10月份开始在这家公司实习,到今年10月份正好一年想(实习+试用期),在想要不要提前9月份就离职,这样好找工作些,但又差一个月满一年,又怕10月份国庆回来离职,容易错过了下半年的金九银十,到年底容易gap到年后
小破站_程序员YT:说这家公司不好吧,你干了快一年 说这家公司好吧,你刚毕业就想跑路说你不懂行情吧,你怕错过金九银十说 你懂行情吧,校招阶段在实习,毕业社招想换工作 哥们,我该怎么劝你留下来呢
应届生,你找到工作了吗
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务