Java - 如何在 switch 语句中调用方法?

averageLength获取用户在上一个案例中输入的所有单词并获取平均单词数。switch 语句位于 main 方法下(此处未显示),但是当我尝试实现案例 3 以获得平均值时,它不起作用,因为它没有average在main方法下声明,而是在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) { 

    }


    public static void averageLength(String[] words, int count) {

        Scanner sc = new Scanner(System.in);

        double average;

        double sum;


        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);


繁花不似锦
浏览 126回答 4
4回答

慕码人2483693

您粘贴的代码存在一些问题 - 其中一些问题已在之前的评论/答案中确定,但为了完整性,我也会在此处列出它们:名称中的拼写错误averageLenth- 更改为averageLength。因此,您的代码将无法编译。averageLength(String[] words, int count)呼叫站点未遵守 的签名- 更改averageLength(words, count)为交换机内的呼叫站点。的实现averageLength是不正确的 - 您的实现实际上并不是迭代单词数组并计算平均值,而是似乎要求扫描仪提供下一个输入。我更改了下面代码中的实现,以通过迭代单词数组来计算平均值。import java.util.Scanner;import java.util.Arrays;/**&nbsp;* Word Manager&nbsp;*&nbsp;* @author Harry&nbsp;*/public class WordManager {/**&nbsp;* Adds the word to the next empty space in the array, if there is space.&nbsp;* Returns the new size of the array.&nbsp;*/public static int add(String[] words, int count, String word) {&nbsp; &nbsp; if (count < words.length) {&nbsp; &nbsp; &nbsp; &nbsp; words[count] = word;&nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The array is full");&nbsp; &nbsp; }&nbsp; &nbsp; return count;}/**&nbsp;* Displays the words in the collection as a comma separated list.&nbsp;*/public static void printList(String[] words, int count) {}private static void averageLength(String[] words, int count) {&nbsp; &nbsp; int sum=0;&nbsp; &nbsp; for(int word =0; word < count; word++){&nbsp; &nbsp; &nbsp;int wordLength = words[word].length();&nbsp; &nbsp; &nbsp;sum += wordLength;&nbsp; &nbsp; }&nbsp; &nbsp; double averageWorldLength = sum/count;&nbsp; &nbsp; System.out.println("Average word length = " +averageWorldLength;}public static void main(String[] args) {&nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; String[] words = new String[20];&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; String aWord;&nbsp; &nbsp; int choice;&nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; displayMenuOptions();&nbsp; &nbsp; &nbsp; &nbsp; choice = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("choice = " + choice);&nbsp; &nbsp; &nbsp; &nbsp; switch (choice) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Add a word");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aWord = sc.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = add(words, count, aWord);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Display words");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("We have an array of " + words.length + " integers: " + Arrays.toString(words));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; averageLength(words, count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid responce");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } while (choice >= 0 && choice < 4);}private static void displayMenuOptions() {&nbsp; &nbsp; System.out.println("\t MENU:");&nbsp; &nbsp; System.out.println("1. Add a word");&nbsp; &nbsp; System.out.println("2. Display words:");&nbsp; &nbsp; System.out.println("3. Display average word length");&nbsp; &nbsp; System.out.println("4. Quit");&nbsp; &nbsp; System.out.println("Enter option: ");&nbsp;}}

慕姐4208626

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

至尊宝的传说

从显示的代码中,您可以不带参数调用“averageLength()”,而它需要两个参数:单词数组及其计数。该调用还包含一个拼写错误(缺少“g”)。因此,编译器无法找到该函数,因为它引用了实际不存在的函数。此外,在“averageLength()”的两个参数中,单词数组未使用,您可以重新扫描单词,而不是使用通过其他开关情况建立的列表。这很可能是一个逻辑错误。

明月笑刀无情

请修复:在方法中main():averageLenth();averageLength(words, count);在方法中averageLength():double sum;double sum = 0;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java