看了一篇NET考試的試卷,發現下面的java題
public class First {
public static int CBSE(int x) {
if (x < 100) x = CBSE(x 10);
return (x - 1);
}
public static void main(String[] args) {
System.out.print(First.CBSE(60));
}
}
我無法理解 if 條件。我在我的機器上運行程式,它回傳 95 沒有任何錯誤。誰能演示一下程式是如何執行的?
uj5u.com熱心網友回復:
假設第二行的 10 是 x
CBSE(60)
=CBSE(70)-1
=CBSE(80)-1-1
=CBSE(90)-1-1-1
=CBSE(100)-1-1-1-1
=99-1-1-1-1
=95
uj5u.com熱心網友回復:
CBSE(60) ->
60<100 so, x=CBSE(70)
...................................80
............................................90
......................................................100
100 is not < 100 SO CBSE(100) Returns 100-1=99
cbse(90) returns 98
cbse(80) returns 97
cbse(70) 回傳 96
cbse(60) 回傳 95
因此最終結果是 95
uj5u.com熱心網友回復:
您使用遞回函式。
當 x<100 時,任何新呼叫都會被堆疊。
如果您將跟蹤放在 BSE 函式中,您將看到:
entering CBSE with 60
entering CBSE with 70
entering CBSE with 80
entering CBSE with 90
entering CBSE with 100
exiting CBS with 99
exiting CBS with 98
exiting CBS with 97
exiting CBS with 96
exiting CBS with 95
95
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/476191.html
上一篇:Python中的if陳述句有問題
