基本上我的任務是獲得一堆數組中的元素,並與他們一起做各種任務,如找到最短/最長的元素等。構造函數錯誤。谷歌沒有幫助
現在我所堅持的可能是錯誤的我的構造函數。
我所要做的就是創建一個具有單個字符串數組作爲參數的構造函數。
我想要做的是獲得一個句子中單詞的平均長度。到目前爲止我的代碼:
public class Sentence {
private static String[] words = new String[50]; //Instance variable that holds words.
public Sentence(String[] array){ // My Constructor. (If that is right?)
}
public double meanLength(String str) { //Method for sorting words.
String words[] = str.split("()");
int numWords = words.length;
int totalCharacters = 0;
for (int i = 0; i < numWords; i++)
totalCharacters = totalCharacters + words[i].length();
return totalCharacters/numWords;
}
public static void main(String[] args) {
String[] wordList = {"A", "quick", "brown", "fox", "jumped",
"over", "the", "lazy", "dog"};
Sentence text = new Sentence(wordList);
System.out.printf("Mean word length:%5.2f\n",text.meanLength());
// Now this is where the error comes up.
錯誤說:類句子 Java方法meanLength不能應用於給定類型;
要求:java.lang.String中
發現:沒有參數
原因:實際的和正式的參數列表長度
不同會感謝所有幫助。可能有更多的錯誤需要解決,因爲我是一個非常新的Java程序員。比如當我取消排序的時候。系統打印時有許多空白。
謝謝。
請張貼您的實際代碼。你缺少了一堆{} – njzk2
你似乎在試圖定義構造函數中的一個實例方法。這是行不通的。 – khelwood
@khelwood是的,我有一堆其他的代碼,所以我縮短了它極大地 – bob9123