亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

遞歸程序有一個帶有示例方法的 StackOverflowError

遞歸程序有一個帶有示例方法的 StackOverflowError

喵喵時光機 2021-08-25 11:09:12
我創建了一個遞歸程序來多次打印消息 Hello,但在編譯中因 StackOverflowError 而失敗。package com.recre;public class Recursionhello {    static void p() {        System.out.println("Hello");    p();}public static void main(String[] args) {    p();    }}它多次打印輸出“Hello”,然后打印以下錯誤消息。輸出 -HelloHelloHelloHelloHelloHelloHelloHelloException in thread "main" java.lang.StackOverflowError at java.io.FileOutputStream.write(Unknown Source) at java.io.BufferedOutputStream.flushBuffer(Unknown Source) at java.io.BufferedOutputStream.flush(Unknown Source) at java.io.PrintStream.write(Unknown Source) at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source) at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source) at sun.nio.cs.StreamEncoder.flushBuffer(Unknown Source) at java.io.OutputStreamWriter.flushBuffer(Unknown Source) at java.io.PrintStream.write(Unknown Source) at java.io.PrintStream.print(Unknown Source) at java.io.PrintStream.println(Unknown Source) at com.recre.Recursionhello.p(Recursionhello.java:5) at com.recre.Recursionhello.p(Recursionhello.java:6) at com.recre.Recursionhello.p(Recursionhello.java:6) at com.recre.Recursionhello.p(Recursionhello.java:6) at com.recre.Recursionhello.p(Recursionhello.java:6) at com.recre.Recursionhello.p(Recursionhello.java:6) at com.recre.Recursionhello.p(Recursionhello.java:6) at com.recre.Recursionhello.p(Recursionhello.java:在這里,我需要有關錯誤的助手和有關遞歸的一些解釋。
查看完整描述

2 回答

?
catspeake

TA貢獻1111條經驗 獲得超0個贊

Java 中的任何遞歸程序都必須具有退出條件,如下所示:


public class Recursionhello {

    static void p(int times) {

        System.out.println("Hello");

        if(times > 0) {

            p(times - 1);

        }

    }


    public static void main(String[] args) {

        p(5);

    }

}


查看完整回答
反對 回復 2021-08-25
  • 2 回答
  • 0 關注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號