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

标签 java variable-declaration

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

            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:
                    averageLenth();              
                    break;

                default:
                    System.out.println("Invalid responce");

            }
        } while (choice >= 0 && choice < 4);

    }
}

最佳答案

您粘贴的代码存在一些问题 - 其中一些问题已在之前的评论/答案中找到,但为了完整起见,我也会在此处列出它们:

  1. averageLenth 名称中的拼写错误 - 更改为 averageLength。因此,您的代码将无法编译。
  2. 调用站点未遵循 averageLength(String[] Words, int count) 的签名 - 在调用时更改为 averageLength(words, count)位于交换机内部。
  3. 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: ");
     }
    }
    

关于Java - 如何在 switch 语句中调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58231868/

相关文章:

java - 在 Java 中使用管道的意外结果

javascript var语句和性能

c - C 中数组声明和定义的时间复杂度是多少?

javascript - 生成 x 个对象

vba - Excel-VBA : Variable declaration necessary?

java - do while 循环后无法继续执行 main 方法

java - 从 Spring Rest api 中的 JPA 查询返回列表

java - 关于java类嵌套的一个疑问

java - 在 Java 中,当方法具有基类作为参数时,使用反射调用方法无法找到方法

java - 无法从字符串创建 Path 对象