文章詳情頁
java - 關(guān)于遞歸方法的問題
瀏覽:133日期:2024-01-01 15:56:46
問題描述
為什么最后會報錯?
public static int count(int n){int result;result = n/4+count(n-4);if (n<0) result = 0;return result; }public static void main(String[] args){System.out.println(count(5)); }
Exception in thread 'main' java.lang.StackOverflowError
at huam.count(huam.java:4)at huam.count(huam.java:4)at huam.count(huam.java:4)at huam.count(huam.java:4)
問題解答
回答1:public static int count(int n){if (n<0) return 0;return n/4+count(n-4); }
標(biāo)簽:
java
相關(guān)文章:
1. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個是怎么回事????2. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項3. html按鍵開關(guān)如何提交我想需要的值到數(shù)據(jù)庫4. css - width設(shè)置為100%之后列表無法居中5. ios - vue-cli開發(fā)項目webstrom會在stylus樣式報錯,飆紅,請大神幫忙6. css3 - 怎么感覺用 rem 開發(fā)的不多啊7. python - 在pyqt中做微信的機器人,要在表格中顯示微信好友的名字,卻顯示不出來,怎么解決?8. html5 - 用Egret寫的小游戲,怎么分享到微信呢?9. javascript - 一個頁面有四個圖片,翻頁的時候想固定住某個圖片然后翻頁,如何實現(xiàn)呢?10. objective-c - 自定義導(dǎo)航條為類似美團的搜索欄樣式
排行榜

熱門標(biāo)簽