Java 如何从同一类中的另一个方法调用变量,以及调用该方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19411041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I call for variables from another method in same class, as well as call upon the method?
提问by Makri
I am struggling to understand the concept of declaring and calling upon classes as well as the traffic of variables between them. In the code I have now I believe there are several problems, but the main one right now is that the main-method isn't able to use the returned value from another method, I do believe I got the rest right. The code:
我正在努力理解声明和调用类的概念以及它们之间变量的流量。在我现在拥有的代码中,我相信有几个问题,但现在主要的问题是 main-method 无法使用其他方法的返回值,我相信我做对了其余的事情。编码:
import java.util.*;
import java.io.*;
class Uke63{
public static void main(String[]args){
char c='t';
String tekst="Jabba the Hutt var en stygg fyr";
forekommer(c, tekst);
System.out.println(retval.forekommer);
}
static boolean forekommer(char c, String tekst){
boolean retval=tekst.contains(c);
return retval;
}
}
And the error I get when trying to compile: (I do see that it doesn't find the value, but I need some help on how to make it find that particular value.)
以及我在尝试编译时遇到的错误:(我确实看到它没有找到该值,但我需要一些关于如何使其找到该特定值的帮助。)
makri@Marius-samlap:~/Programmering/Ukeoppgaver$ javac Uke63.java
Uke63.java:12: error: cannot find symbol
System.out.println(retval.forekommer);
^
symbol: variable retval
location: class Uke63
Uke63.java:18: error: method contains in class String cannot be applied to given types;
boolean retval=tekst.contains(c);
^
required: CharSequence
found: char
reason: actual argument char cannot be converted to CharSequence by method invocation conversion
2 errors
采纳答案by Algorithmist
Modify your code to this
将您的代码修改为此
import java.util.*;
import java.io.*;
class Uke63{
public static void main(String[]args){
char c='t';
String tekst="Jabba the Hutt var en stygg fyr";
System.out.println(forekommer(c, tekst));
}
static boolean forekommer(char c, String tekst){
boolean retval=tekst.contains(c);
return retval;
}
}
Problems in Original Code:You are trying to access local variable declared in forekommer() method in main which is not possible.
原始代码中的问题:您试图访问在 main 中的 forekommer() 方法中声明的局部变量,这是不可能的。
回答by Todoy
The problem is that in your main method you do not have access to "retval". After you call your forekommer method you need to assign the result to a local variable. for example:
问题是在您的主要方法中,您无权访问“retval”。在您调用您的 forekommer 方法后,您需要将结果分配给一个局部变量。例如:
boolean someVarName =forekommer(c, tekst);
System.out.println(someVarName );
回答by bix
Here you go, refined some of the answers here and made the code compile and run.
给你,在这里提炼一些答案并使代码编译和运行。
public class Uke63{
public static void main(String[]args){
CharSequence c= "t";
String tekst="Jabba the Hutt var en stygg fyr";
System.out.println(forekommer(c, tekst));
}
private static boolean forekommer(CharSequence c, String tekst){
return tekst.contains(c);
}
}
回答by Rushikesh Chawan
public class Basics {
public static void main(String[] args) {
CharSequence c = "t";
String tekst="Jabba the Hutt var en stygg fyr";
boolean x = forekommer(c, tekst);
System.out.println(x);
}
static boolean forekommer(CharSequence c, String tekst){
CharSequence x = c;
boolean retval = tekst.contains(x);
return retval;
}
}