所以我的作業只問我類方法,但它需要我打開.java和.class文件。以及我有理論上應該工作的代碼,但無論我嘗試什麼,它都不會編譯。比如這裏有一個:在java中編譯類方法
public class findFourLetterWord(String[] strings) {
public static void main(String[] args) {
for (int i = 0; i < strings.length; i++)
if (strings[i].length()==4)
return strings[i];
return null;
}
}
,這裏是我得到的錯誤:
8 errors found:
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 9]
Error: The public type findFourLetterWord must be defined in its own file
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 9]
Error: Syntax error on token "(", { expected
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 9]
Error: Syntax error on token ")", ; expected
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 9]
Error: Syntax error, insert "}" to complete Block
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 11]
Error: Cannot make a static reference to the non-static field strings
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 12]
Error: Cannot make a static reference to the non-static field strings
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 13]
Error: Cannot make a static reference to the non-static field strings
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 14]
Error: Void methods cannot return a value
有什麼建議?
Ouch。從類聲明中刪除參數。 'public class FindFourLetterWord {...}'同時大寫類名的第一個字母 –
另外,FindFourLetterWord.java以大寫字母F開頭,類名正常f。公共類和.java文件的名稱必須匹配。 –
快速Google搜索提出[this](http://www.roseindia.net/javatutorials/manypublicclassesinonefile.shtml)。 :P – Miguel