简体   繁体   English

无法修复java找不到符号错误

[英]Can't fix java cannot find symbol error

I'm working on a project and I can't figure out how to fix this error.我正在做一个项目,但我不知道如何解决这个错误。 I am calling for an integer from a different method with我正在从不同的方法中调用一个整数

public static void main(String [] args){
    int foo = 0;
    foo = method(foo);
}
public static int method(){
    int foo = (equation)
    return foo;
}

but I keep getting the error:但我不断收到错误消息:

error: cannot find symbol
        foo = method(foo);
                     ^
  symbol:   variable foo
  location: class MainClass

I declared foo as an int in a separate method and I returned foo at the end of it.我在一个单独的方法中将 foo 声明为一个 int ,并在它的末尾返回了 foo 。 But when trying to call for it in the main method I got that error.但是当试图在 main 方法中调用它时,我得到了那个错误。

You can't say你不能说

int foo = method(foo);

because there is no value assigned to foo which might be passed to method .因为没有分配给foo值可能会传递给method You could do something like,你可以做类似的事情,

int foo = 0; // <-- Initialize foo to 0
foo = method(foo); // <-- passes foo

When you declare your method, first (equation) isn't valid code.当你声明你的方法时,首先(equation)不是有效的代码。 Second, you aren't declaring that it takes an int argument.其次,您没有声明它需要一个int参数。 Change method like改变method

public static int method(int foo){
  int x = foo * foo; // <-- equation?
  return x;
}

Or,或者,

int foo = method(); // if method takes no arguments.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM