Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
找不到符号变量java_Java - Fatal编程技术网

找不到符号变量java

找不到符号变量java,java,Java,我试图在Java中编写以下程序,其中我编写了一个递归和迭代方法,以计算从n到m的所有奇数之和 导入java.util.Scanner 公共类分配Q7{ 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

我试图在
Java
中编写以下程序,其中我编写了一个递归和迭代方法,以计算从
n
m
的所有奇数之和

导入java.util.Scanner

公共类分配Q7{

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));
}
 }
public static int recursivesum(int n,int m){
if(nrecursivesum(int n,int m)
方法中的(int i=n;i),您已经在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;
}
public static int recursivesum(int n,int m){
int s=n;//现在s具有方法局部作用域
if(n
递归求和(int n,int m)
函数中,
变量s
的范围在
if and else
块内。当您返回
s
时,它超出范围

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

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;
ints;
if(n
您在
if
语句内部声明变量
s
,这就是为什么会出现这样的错误。从声明
int s
语句外部
if
开始。

此方法就是问题所在:

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!
}

请注意,您在
iterativesum
中遇到了相同的问题-编译器此时应该向您抱怨并非所有路径都返回值。您希望
iterativesum(3,3)
做什么?

您的
s
超出了
recursivesum(int n,int m)的范围
method

if-else
块之外声明
s
这是范围问题。在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 recursivesum(int n,int m){
int s=0;
if(n
迭代方法为:

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;
}
publicstaticintiterativesum(intn,intm){
int s=0;
if(n对于(int i=n;i,在第一个方法中定义
s
时,在返回它的范围之外有一个错误。在第二个方法中,在循环内部返回

正如本线程中的其他人所建议的,使用像Eclipse()或IntelliJ()这样的IDE

import java.util.Scanner;
公共类分配Q7{
公共静态int递归和(int n,int m){
int s=n;
if(n对于(int i=n;i您的代码必须是这样的,您不必为循环使用两个

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));
    }
}
import java.util.Scanner;
公共类分配Q7{
公共静态整数递归和(final int n,final int m){
最终整数下限=nm?n:m;
最终整数总计=较低;
如果(下部>=上部){
返回总数;
}
返回总计+赋值Q7.递归求和(下+2,上);
}
公共静态整数迭代和(final int n,final int m){
最终整数下限=nm?n:m;
int-total=0;

对于(int num=lower;s的num范围在大括号中…在类外部定义它variable@VinayVeluri:不必是类变量。请在if之外声明s变量condition@JonSkeettrue!!返回前作用域未结束的任何变量。
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;
}
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));
  }
}
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));
    }
}