4 回答

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊
您粘貼的代碼存在一些問題 - 其中一些問題已在之前的評(píng)論/答案中確定,但為了完整性,我也會(huì)在此處列出它們:
名稱中的拼寫錯(cuò)誤
averageLenth
- 更改為averageLength
。因此,您的代碼將無(wú)法編譯。averageLength(String[] words, int count)
呼叫站點(diǎn)未遵守 的簽名- 更改averageLength(words, count)
為交換機(jī)內(nèi)的呼叫站點(diǎn)。的實(shí)現(xiàn)
averageLength
是不正確的 - 您的實(shí)現(xiàn)實(shí)際上并不是迭代單詞數(shù)組并計(jì)算平均值,而是似乎要求掃描儀提供下一個(gè)輸入。我更改了下面代碼中的實(shí)現(xiàn),以通過(guò)迭代單詞數(shù)組來(lái)計(jì)算平均值。
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貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊
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貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊
從顯示的代碼中,您可以不帶參數(shù)調(diào)用“averageLength()”,而它需要兩個(gè)參數(shù):?jiǎn)卧~數(shù)組及其計(jì)數(shù)。
該調(diào)用還包含一個(gè)拼寫錯(cuò)誤(缺少“g”)。
因此,編譯器無(wú)法找到該函數(shù),因?yàn)樗昧藢?shí)際不存在的函數(shù)。
此外,在“averageLength()”的兩個(gè)參數(shù)中,單詞數(shù)組未使用,您可以重新掃描單詞,而不是使用通過(guò)其他開關(guān)情況建立的列表。這很可能是一個(gè)邏輯錯(cuò)誤。

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
請(qǐng)修復(fù):
在方法中
main()
:averageLenth();
averageLength(words, count);
在方法中
averageLength()
:double sum;
double sum = 0;
添加回答
舉報(bào)