2014-03-25 60 views
0

我試圖做下面的程序在Java,我正在寫一個遞歸計算從n所有的奇數的總和m找不到符號 - 可變的Java

進口的Java迭代方法.util.Scanner;

公共類AssignmentQ7 {

public static int recursivesum(int n, int m){ 

    if (n < m){ 
     int s = n; 
     s += recursivesum(n+2, m); 

    } else{ 
     if(m < n){ 
      int s = m; 
      s += recursivesum(m+2, n); 

     } 
    } 
    return s; 
} 

public static int iterativesum(int n, int m){ 
    if(n < m){ 
     int s = n; 
     for(int i = n; i <= m; i += 2){ 
      s += i; 
        return s; 
     } 
    } else 
     if(m < n){ 
      int s = m; 
      for(int i = m; i <= n; i += 2){ 
      s += i; 
        return s; 
      } 
     } 

} 

public static void main(String args[]){ 

    int n,m; 

    Scanner in = new Scanner(System.in); 

    System.out.println("Enter two numbers: "); 
    n = in.nextInt(); 
    m = in.nextInt(); 

    while(n%2 == 0){ 
     System.out.println("Enter the first number again: "); 
     n = in.nextInt(); 
    } 

    while(m%2 == 0){ 
     System.out.println("Enter the second number again: "); 
     m = in.nextInt(); 
    } 

    System.out.println("The answer of the recursive sum is: " + recursivesum(n,m)); 
    System.out.println("The answer of the iterative sum is: " + iterativesum(n,m)); 
} 
} 

我得到一個錯誤,無法找到符號 - 可變enter code here秒。我不知道什麼是錯的!任何人都可以幫忙嗎?

+1

範圍是在括號外...它定義爲類變量 – VinayVeluri

+0

@VinayVeluri:沒有必要爲它是一個類變量。 –

+0

在你的條件之外聲明你的變量 – David

回答

2

recursivesum(int n, int m)方法中,如果if條件聲明瞭s,但是,您試圖在其他部分訪問它。

public static int recursivesum(int n, int m){ 
    int s = n; // Now s having method local scope 

    if (n < m){ 
     s += recursivesum(n+2, m); 

    } else{ 
     if(m < n){ 
      int s = m; 
      s += recursivesum(m+2, n); 

     } 
    } 
    return s; 
} 
0

recursivesum(int n,int m)起作用的variable s範圍是if and else塊內。當你退回s它超出範圍。

嘗試使用一些IDE,如eclipse。所以,你可以調試這些錯誤立即

0

試試這個:

int s; 
if (n < m){ 
     s = n; 
     s += recursivesum(n+2, m); 

    } else{ 
     if(m < n){ 
      int s = m; 
      s += recursivesum(m+2, n); 

     } 
    } 
    return s; 
0

您聲明變量sif說法,這就是爲什麼你得到這樣的錯誤。外部if聲明int s聲明開始。

5

這種方法的問題是:

public static int recursivesum(int n, int m) { 
    if (n < m) { 
     int s = n; // Variable declared here... scoped to the if 
     s += recursivesum(n+2, m); 

    } else { 
     if (m < n) { 
      int s = m; // Variable declared here... scoped to this if 
      s += recursivesum(m+2, n); 
     } 
    } 
    return s; // No such variable in scope! 
} 

你可以使用:

public static int recursivesum(int n, int m) { 
    int s = 0; // See note below 
    if (n < m) { 
     s = n + recursivesum(n+2, m); 

    } else { 
     if (m < n) { 
      s = m + recursivesum(m+2, n); 
     } 
    } 
    return s; 
} 

我們必須讓s明確的初始值,因爲你目前沒有任何代碼辦案其中nm相等。目前還不清楚你想要做什麼。

另一種方法是從if語句返回,就像你做iterativesum ......雖然你再次需要考慮如果m == n做什麼:

public static int recursivesum(int n, int m) { 
    if (n < m) { 
     // You don't even need an s variable! 
     return n + recursivesum(n+2, m); 
    } else if (m < n) { 
     // Simplified else { if { ... } } to else if { .... } 
     return m + recursivesum(m+2, n); 
    } else { 
     // What do you want to return here? 
    } 
} 

請注意,已在iterativesum中得到了同樣的問題 - 編譯器應該在此時抱怨你並不是所有的路徑都返回一個值。你期望iterativesum(3, 3)能做什麼?

0

s超出範圍在recursivesum(int n, int m)方法

0

聲明sif-else

0

它的範圍之外的問題。你在變量的(局部定義)if語句中聲明變量s。

您需要修改這兩個方法如下:

遞歸方法將

public static int recursivesum(int n, int m) { 
    int s = 0; 
    if (n < m) { 
     s = n; 
     s += recursivesum(n + 2, m); 
    } else { 
     if(m < n){ 
      s = m; 
      s += recursivesum(m + 2, n); 
     } 
    } 
    return s; 
} 

而且迭代方法是:

public static int iterativesum(int n, int m) { 
    int s = 0; 
    if(n < m) { 
     s = n; 
     for(int i = n; i <= m; i += 2) { 
      s += i; 
     } 
    } else { 
     if(m < n) { 
      s = m; 
      for(int i = m; i <= n; i += 2) { 
       s += i; 
      } 
     } 
    } 
    return s; 
} 
0

您在有錯誤第一種方法,在您將其返回的範圍之外定義s。在循環內返回第二種方法。

正如其他人在這個線程表明,使用如Eclipse(https://www.eclipse.org/)或的IntelliJ(http://www.jetbrains.com/idea/)的IDE

import java.util.Scanner; 

public class AssignmentQ7 { 

    public static int recursivesum(int n, int m) { 
    int s = n; 
    if (n < m) { 
     s += recursivesum(n + 2, m); 
    } 
    else { 
     if (m < n) { 
     s = m; 
     s += recursivesum(m + 2, n); 
     } 
    } 
    return s; 
    } 

    public static int iterativesum(int n, int m) { 
    int s = 0; 
    if (n < m) { 
     for (int i = n; i <= m; i += 2) { 
     s += i; 
     } 
    } 
    else if (m < n) { 
     for (int i = m; i <= n; i += 2) { 
     s += i; 
     } 
    } 
    return s; 
    } 

    public static void main(String args[]) { 

    int n, m; 

    Scanner in = new Scanner(System.in); 

    System.out.println("Enter two numbers: "); 
    n = in.nextInt(); 
    m = in.nextInt(); 

    while (n % 2 == 0) { 
     System.out.println("Enter the first number again: "); 
     n = in.nextInt(); 
    } 

    while (m % 2 == 0) { 
     System.out.println("Enter the second number again: "); 
     m = in.nextInt(); 
    } 

    System.out.println("The answer of the recursive sum is: " + recursivesum(n, m)); 
    System.out.println("The answer of the iterative sum is: " + iterativesum(n, m)); 
    } 
} 
0

你的代碼必須是這樣的,你不必須使用兩個for loop。 S的

import java.util.Scanner; 

public class AssignmentQ7 { 

    public static int recursivesum(final int n, final int m) { 

     final int lower = n < m ? n : m; 
     final int upper = n > m ? n : m; 
     final int total = lower; 

     if (lower >= upper) { 
      return total; 
     } 
     return total + AssignmentQ7.recursivesum(lower + 2, upper); 
    } 

    public static int iterativesum(final int n, final int m) { 

     final int lower = n < m ? n : m; 
     final int upper = n > m ? n : m; 

     int total = 0; 
     for (int num = lower; num <= upper; num = num + 2) { 
      total += num; 
     } 

     return total; 
    } 

    public static void main(final String args[]) { 

     int n, m; 

     final Scanner in = new Scanner(System.in); 

     System.out.println("Enter two numbers: "); 
     n = in.nextInt(); 
     m = in.nextInt(); 

     while (n % 2 == 0) { 
      System.out.println("Enter the first number again: "); 
      n = in.nextInt(); 
     } 

     while (m % 2 == 0) { 
      System.out.println("Enter the second number again: "); 
      m = in.nextInt(); 
     } 

     System.out.println("The answer of the recursive sum is: " + AssignmentQ7.recursivesum(n, m)); 
     System.out.println("The answer of the iterative sum is: " + AssignmentQ7.iterativesum(n, m)); 
    } 
}