找不到符号 - 变量java

时间:2014-03-25 09:33:40

标签: java

我正在尝试在Java中执行以下程序,其中我正在编写一个递归和迭代方法来计算从nm的所有奇数之和

  

import 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 s。我不知道出了什么问题!有人可以帮忙吗?

10 个答案:

答案 0 :(得分: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:< / p>

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)做什么?

答案 1 :(得分: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;
}

答案 2 :(得分:0)

recursivesum(int n,int m)函数中,variable s的范围位于if and else块内。当您返回s时,它超出了范围。

尝试使用某些IDE,例如eclipse。这样您就可以立即调试这些错误

答案 3 :(得分: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;

答案 4 :(得分:0)

您在s语句中声明变量if,这就是您遇到此类错误的原因。从int s声明之外的声明if开始。

答案 5 :(得分:0)

您的s超出了recursivesum(int n, int m)方法

的范围

答案 6 :(得分:0)

s

之外声明if-else

答案 7 :(得分: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;
}

答案 8 :(得分: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));
  }
}

答案 9 :(得分:0)

您的代码必须是这样的,您不必使用两个for loop

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));
    }
}