2016-03-28 279 views
-5

這裏是第一類有人可以解釋這個代碼

公共類Number {

private static int Result; 

public Number(int X){ 

if(X == 0){ return; } 

Result += X; 

Number another_number = new Number(X-1); 

} 

public static void Clear(){ 

Result = 0; 

} 

public static int getResult(){ 

return Result; 

} 

}

這裏是第二類 公共靜態無效的主要(字串[] args){

Number.Clear(); 

    Number x = new Number(5); 

    System.out.println("The result is " + Number.getResult()); 

    Number y = new Number(5); 

    System.out.println("The result is " + Number.getResult()); 

    Number.Clear(); 

    Number z = new Number(5); 

    System.out.println("The result is " + Number.getResult()); 

    } 

起初我以爲答案是:

The result is 9. (Because 10-1 is 9) 
The result is 13. (Because 9+5 -1 is 13) 
The result is 4. (Because 5-1 is 4). 

但實際上答案是:

The result is 55 
The result is 70 
The result is 15 

這難倒我。我知道我錯過了一些大概念,但我無法弄清楚。我已經和另外3個人交談過,他們也很難過。如果有人能幫助我們,那會很棒。

+1

您應該逐個調試器中的代碼,看看實際發生了什麼。 –

+1

您確定此代碼生成55,70,15嗎?我預計它會產生15,30,15。你是否複製/粘貼錯誤的東西?無論哪種方式,你對「10-1是9」等事物的推理完全不相干。代碼無處不在。代碼*正在做的是遞歸計數並將每個結果添加到靜態總數中。然後打印總計。 5 + 4 + 3 + 2 + 1是15.(當然,15 + 5 + 4 + 3 + 2 + 1是30.) – David

+0

我期望它產生15,15和15.注意重置每次通話之間的靜態 – tddmonkey

回答

2

當您實例化一個Number對象時,將遞歸調用Number(int X)構造函數。

public Number(int X) 
{ 
    if (X == 0) { return; } 

    //This will increase result by X 
    Result += X; 

    //This will instantiate a new Number which will call back into this constructor 
    Number another_number = new Number(X - 1); 

} 

由於每個構造函數被調用時,我們增加static變量Result我們會從1到X添加所有號碼Result。所以,任何時候你創建的主類結果的新Number對象基本上是由下式表示:

Result = Result + X + (X-1) + (X-2) + ... +1 

由於主要開始於Number.Clear()Result會從0開始。然後當main方法被調用,它創建的第一Number對象的結果應該是15:

Number x = new Number(5); //This will result in Result = 0 + 5 + 4 + 3 + 2 + 1 

當所述第二數量是由,Result應爲30:

Number y = new Number(5); //This will result in Result = 15 + 5 + 4 + 3 + 2 + 1 

最後,我們打電話到Number.Clear()並創建一個新Number對象:

Number z = new Number(5); //This will result in Result = 0 + 5 + 4 + 3 + 2 + 1 
1

你缺少大的概念是構造函數調用本身在該行

Number another_number = new Number(X-1); // new Number calls Number constructor. 

所以,如果你新Number(5),該階段如下

Result += 5;  // Result is 5 
new Number(4); 
Result += 4;  // Result is 9 
new Number(3); 
Result += 3;  // Result is 12 
new Number(2); 
Result += 2;  // Result is 14 
new Number(1); 
Result += 1;  // Result is 15 
new Number(0); // stops because X == 0 

(順便說一句,您提到的結果是指在編輯之前問題的原始版本)。

+0

*問題的原始版本*?就我所見,編輯只改變了格式。 – Gendarme

+0

@Gendarme我現在很困惑。我確信我看到了一個版本,其中「新數字」被調用的數字不是5。要麼我瘋了,要麼有一條規則,如果編輯速度足夠快,原始文件不會顯示在編輯歷史中。 –

+0

我以爲我也看到了,但它不在編輯歷史 – tddmonkey

0

您正在從構造函數更新一個靜態int。

我不明白你的價值。我看15,30,15

每次調用構造函數時,使用的是遞歸,直到你達到0

在第一個構造函數調用添加遞減一系列數字,您要添加5, 4,3,2,1得到15的結果。然後在不清除結果的情況下,再次添加5,4,3,2,1得到總數30.然後總和被清除,並且第一個結果再次看到15。

你的代碼註釋:

private static int Result; // static, so the value is kept between initialised objects. 

public Number(int X) 
{ 

    if (X == 0) 
    { 
     return; // Return, and so end the recursion. 
    } 

    Result += X; // Add x to the result 

    Number another_number = new Number(X - 1); // Recurse into the constructor with x-1 

} 
1

每次調用構造函數,靜態場Result通過參數的值遞增。之後,構造函數會再次調用自己,但參數少一個。重複這個過程,直到達到0的參數後,什麼也沒有發生。

所以,當你寫new Number(5),您可以通過5增加Result,然後通過4,然後通過3,然後通過2,最後由1。所以你總共增加了Result151+2+3+4+5)。

當您撥打Number.Clear()時,您將Result的值重置爲0。因爲它是第一個施工前重置了,第三過,但是沒有前一秒,你的輸出應該是這樣的:

The result is 15 
The result is 30 
The result is 15 

如果你正在

The result is 55 
The result is 70 
The result is 15 

它意味着你有寫new Number(10)第一次和new Number(5)第二次和第三次。

-1

公共靜態無效的主要(字串[] args)是不是第二個「類」,它是類號碼的主要方法。

如果你把第二個「類」公共靜態無效的主要(字串[] args){...}類號內部並運行它,結果如下:

The result is 15 
The result is 30 
The result is 15 

的正確的代碼是

class Number { 
     private static int Result; 

     public Number(int X){ 
     if(X == 0){ return; } 
     Result += X; 
     Number another_number = new Number(X-1); 
     } 

     public static void Clear(){ 
     Result = 0; 
     } 

     public static int getResult(){ 
     return Result; 
     } 

     public static void main(String[] args) { 
     Number.Clear(); 
     Number x = new Number(5); 
     System.out.println("The result is " + Number.getResult()); 
     Number y = new Number(5); 
     System.out.println("The result is " + Number.getResult()); 
     Number.Clear(); 
     Number z = new Number(5); 
     System.out.println("The result is " + Number.getResult()); 
     } 
    } 
相關問題