它反映了一个错误,当我使用函数generatecode()时,我无法使用静态函数,我想看看是否正确地进行了拆分。我是新手,仍然需要一些帮助。在这种情况下,我已经看到了有关通过创建新类的一些信息:TestFile variable = new TestFile();我不知道这意味着什么。谢谢!
public class TestFile {
String[] preps = {
"about", "above", "across", "after", "against",
"along", "among", "around", "at", "before",
"behind", "below", "beneath", "beside", "between",
"by", "concerning", "down", "during", "except",
"for", "from", "in", "inside", "into",
"like", "near", "of", "onto", "out",
"over", "through", "to", "toward", "under",
"up", "upon", "with", "within", "without"
};
String[] errorpreps = {
"will", "would", "shall", "should", "can",
"could", "may", "might", "must",
};
String[] question = {
};
public static void main(String[] args) {
generatecode("hi");
};
public generatecode(String code){
String prep = "";
for (int i=0; i<preps.length; i++){
prep = prep + preps[i];
}
System.out.println(prep);
return prep;
}
public String printcode(String code){
return "";
}
}
最佳答案
在您的static
main
方法中,您还没有TestFile
类的任何实例。要引用非static
以外的任何内容,您需要一个该类的实例。这正是行TestFile variable = new TestFile();
所做的-它创建了TestFile
的新实例。
然后,您可以在实例上调用方法:
variable.generatecode("hi");
正如@ChrisCooney已经指出的那样,您没有该方法的返回类型。所有方法都需要一个返回类型。在这种情况下,您需要声明您的方法返回
String
,因为这正是该方法返回的内容。