文章詳情頁
java - 關(guān)于遞歸方法的問題
瀏覽:102日期: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); }
標簽:
java
相關(guān)文章:
1. javascript - 在 vue里面用import引入js文件,結(jié)果為undefined2. php - 微信開發(fā)驗證服務(wù)器有效性3. javascript - 我的站點貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?4. javascript - js setTimeout在雙重for循環(huán)中如何使用?5. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時間會消失是什么情況?6. 小程序怎么加外鏈,語句怎么寫!求救新手,開文檔沒發(fā)現(xiàn)7. python沒入門,請教一個問題8. html - 爬蟲時出現(xiàn)“DNS lookup failed”,打開網(wǎng)頁卻沒問題,這是什么情況?9. java - idea創(chuàng)建多modules項目后,tomcat啟動失敗10. php - mysql 模糊搜索問題
排行榜

熱門標簽