2014-11-25 31 views
0

我在從1^2到n^2遞歸添加Java中的整數時遇到了一些麻煩。 我希望能夠遞歸地在recurvMath方法中做到這一點,但我所得到的是一個無限循環。遞歸添加從1^2到n^2的整數

import java.util.Scanner; 

public class Lab9Math { 

int count = 0; 
static double squareSum = 0; 


public static void main(String[] args){ 

    int n = 0; 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Please enter the value you want n to be: "); 
    n = scan.nextInt(); 

    Lab9Math est = new Lab9Math(); 
    squareSum = est.recurvMath(n); 
    System.out.println("Sum is: "+squareSum); 
} 

public int recurvMath(int n){ 

    System.out.println("N:" +n); 
     if(n == 0){ 
      return 0; 
     }//end if 
     if (n == 1){ 
      return 1; 
     }//end if 
     if (n > 1){ 
      return (recurvMath((int) ((int) n+Math.pow(n, 2)))); 
     }//end if 
     return 0; 
    }//end method  
}//end class 

我不完全掌握遞歸定義這個性質,因爲我知道,我能到這裏:

return (int) (Math.pow(n, 2)); 

,但我不能正確地納入recurvMath方法的調用爲了它的工作。 任何幫助,將不勝感激。謝謝!

+1

爲了達到這個目的,或許你應該把兩件或兩件以上的東西放在一起? – hobbs 2014-11-25 05:42:53

+0

你爲什麼要遞歸地做這件事? 'int s = 0; for(int i = 1; i 2014-11-25 05:44:03

+3

@ElliottFrisch我想了解java中的遞歸。有什麼更好的方法來嘗試編程! – ekep23 2014-11-25 05:50:01

回答

2

一般來說,試圖解決遞歸問題的時候,它有助於嘗試在對它們進行編程之前先將它們寫在腦海中。

你想總結所有整數從1 到n 。我們需要做的第一件事是以適合遞歸的方式表達這一點。好了,說這筆錢的另一種方式是:

  • 所有整數從1 到(n-1)之和,加N

的第一步通常是最難的,因爲它是最「顯而易見的」。例如,我們知道「a + b + c」與「a + b」相同,加上「c」,但是我們必須採取各種信仰的方式,並且說明它是如何進入遞歸的形成。

所以,現在我們必須照顧的特殊基地情況,0:

  • n爲0時,和是0

因此,讓我們讓recurvMath(n)是總和所有整數從1 至n 。然後,將上述直接轉換爲:

  • recurvMath(N)= recurvMath第(n-1)+ N
  • recurvMath(0)= 0

而這是很容易實現:

public int recurvMath(int n){  
    System.out.println("N:" +n); 
    if(n == 0){ 
     return 0; 
    } else { 
     return recurvMath(n-1) + (n * n); 
    } 
} 

注意我選擇去與n * n而不是Math.pow()。這是因爲Math.pow()double上運行,而不是在int上運行。

順便說一句,你可能也想保護自己免受輸入負數的用戶輸入,這可能會讓你卡住。您可以使用if (n <= 0)而不是if (n == 0),或者檢查否定輸入並投擲例如IllegalArgumentException,或甚至適當地使用Math.abs(),並賦予其使用負數的能力。


此外,爲了完整起見,我們來看看原始代碼中的問題。你的問題是:

recurvMath((int) ((int) n+Math.pow(n, 2))) 

讓我們在我們的頭上跟蹤這一點。您的其中一個int劇組是不必要的,但是忽略n == 3這是recurvMath(3 + Math.pow(3, 2))這就是recurvMath(12)。你的號碼每次都會變大。你永遠不會打你1或0的基本情況,所以你永遠不會終止。最終你得到一個整數溢出錯誤的結果或堆棧溢出。

+0

很容易理解,謝謝。 – ekep23 2014-11-25 06:00:37

+0

甚至不需要'else'解決方案:) – Junaid 2014-11-25 06:17:42

0

試試這個

import java.util.Scanner; 

public class Lab9Math { 

int count = 0; 
static double squareSum = 0; 


public static void main(String[] args){ 

    int n = 0; 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Please enter the value you want n to be: "); 
    n = scan.nextInt(); 

    Lab9Math est = new Lab9Math(); 
    squareSum = est.recurvMath(n); 
    System.out.println("Sum is: "+squareSum); 
} 

public int recurvMath(int n){ 

    System.out.println("N:" +n); 
     if(n == 1){ 
      return 1; 
     }//end if 
     // More simplified solution 
     return recurvMath(n-1) + (int) Math.pow(n, 2); // Here is made changes 

    }//end method  
}//end class 
+0

我得到了那麼多,但我似乎無法理解爲什麼我必須發送(n-1)作爲參數而不是n。 – ekep23 2014-11-25 05:48:33

+0

否則堆棧會溢出。意味着你的基本條件將永遠不會發生。或換句話說,你無時無刻都在調用一個函數。如果您發現我的答案有幫助,請將其標記爲正確。 – Junaid 2014-11-25 05:50:54

+0

謝謝@JasonC回答更新。 – Junaid 2014-11-25 05:58:27

0

而不是說的:

return (recurvMath((int) ((int) n+Math.pow(n, 2)))); 

我,而不是說:

return (int) ((Math.pow(n, 2)+recurvMath(n-1)));