4 回答

TA貢獻1860條經驗 獲得超9個贊
您粘貼的代碼存在一些問題 - 其中一些問題已在之前的評論/答案中確定,但為了完整性,我也會在此處列出它們:
名稱中的拼寫錯誤
averageLenth
- 更改為averageLength
。因此,您的代碼將無法編譯。averageLength(String[] words, int count)
呼叫站點未遵守 的簽名- 更改averageLength(words, count)
為交換機內的呼叫站點。的實現
averageLength
是不正確的 - 您的實現實際上并不是迭代單詞數組并計算平均值,而是似乎要求掃描儀提供下一個輸入。我更改了下面代碼中的實現,以通過迭代單詞數組來計算平均值。
import java.util.Scanner;
import java.util.Arrays;
/**
* Word Manager
*
* @author Harry
*/
public class WordManager {
/**
* Adds the word to the next empty space in the array, if there is space.
* Returns the new size of the array.
*/
public static int add(String[] words, int count, String word) {
if (count < words.length) {
words[count] = word;
count++;
} else {
System.out.println("The array is full");
}
return count;
}
/**
* Displays the words in the collection as a comma separated list.
*/
public static void printList(String[] words, int count) {
}
private static void averageLength(String[] words, int count) {
int sum=0;
for(int word =0; word < count; word++){
int wordLength = words[word].length();
sum += wordLength;
}
double averageWorldLength = sum/count;
System.out.println("Average word length = " +averageWorldLength;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] words = new String[20];
int count = 0;
String aWord;
int choice;
do {
displayMenuOptions();
choice = sc.nextInt();
System.out.println("choice = " + choice);
switch (choice) {
case 1:
System.out.println("Add a word");
aWord = sc.next();
count = add(words, count, aWord);
break;
case 2:
System.out.println("Display words");
System.out.println("We have an array of " + words.length + " integers: " + Arrays.toString(words));
break;
case 3:
averageLength(words, count);
break;
default:
System.out.println("Invalid responce");
}
} while (choice >= 0 && choice < 4);
}
private static void displayMenuOptions() {
System.out.println("\t MENU:");
System.out.println("1. Add a word");
System.out.println("2. Display words:");
System.out.println("3. Display average word length");
System.out.println("4. Quit");
System.out.println("Enter option: ");
}
}

TA貢獻1852條經驗 獲得超7個贊
import java.util.Scanner;
import java.util.Arrays;
/**
* Word Manager
*
* @author Harry
*/
public class WrodManager {
/**
* Adds the word to the next empty space in the array, if there is space.
* Returns the new size of the array.
*/
public static int add(String[] words, int count, String word) {
if (count < words.length) {
words[count] = word;
count++;
} else {
System.out.println("The array is full");
}
return count;
}
/** Displays the words in the collection as a comma separated list. */
public static void printList(String[] words, int count) {
}
public static void averageLength(String[] words, int count) {
Scanner sc = new Scanner(System.in);
double average;
double sum = 0;
while (sc.hasNext()) {
String userInput = sc.next();
double charNum = userInput.length();
sum = charNum + sum;
count++;
if (count > 0) {
average = sum / count;
System.out.println("Average word length = " + average);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] words = new String[20];
int count = 0;
String aWord;
int choice;
do {
System.out.println("\t MENU:");
System.out.println("1. Add a word");
System.out.println("2. Display words:");
System.out.println("3. Display average word length");
System.out.println("4. Quit");
System.out.println("Enter option: ");
choice = sc.nextInt();
System.out.println("choice = " + choice);
switch (choice) {
case 1:
System.out.println("Add a word");
aWord = sc.next();
count = add(words, count, aWord);
break;
case 2:
System.out.println("Display words");
System.out.println("We have an array of " + words.length + " integers: " + Arrays.toString(words));
break;
case 3:
averageLength(words, count);
break;
default:
System.out.println("Invalid responce");
}
} while (choice >= 0 && choice < 4);
}
}

TA貢獻1789條經驗 獲得超10個贊
從顯示的代碼中,您可以不帶參數調用“averageLength()”,而它需要兩個參數:單詞數組及其計數。
該調用還包含一個拼寫錯誤(缺少“g”)。
因此,編譯器無法找到該函數,因為它引用了實際不存在的函數。
此外,在“averageLength()”的兩個參數中,單詞數組未使用,您可以重新掃描單詞,而不是使用通過其他開關情況建立的列表。這很可能是一個邏輯錯誤。

TA貢獻1828條經驗 獲得超4個贊
請修復:
在方法中
main()
:averageLenth();
averageLength(words, count);
在方法中
averageLength()
:double sum;
double sum = 0;
添加回答
舉報