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

时间:2019-10-04 07:35:25

标签: java variable-declaration

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

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

    }
}

4 个答案:

答案 0 :(得分:0)

在显示的代码中,您不带参数的情况下调用“ averageLength()”,而它需要两个参数:单词数组及其计数。

该呼叫还包含一个错字(缺少“ g”)。

因此,编译器无法找到该函数,因为它引用了实际上不存在的函数。

此外,在“ averageLength()”的两个参数中,未使用单词数组,您可以重新扫描单词,而不使用通过开关的其他情况建立的列表。 这可能是逻辑错误。

答案 1 :(得分:0)

请修复:

  1. 在方法main()中:averageLenth();averageLength(words, count);
  2. 在方法averageLength()中:double sum;double sum = 0;

答案 2 :(得分:0)

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

    }
}

答案 3 :(得分:0)

您粘贴的代码存在一些问题-其中一些已在以前的注释/答案中得到确认,但出于完整性考虑,我还将在这里列出它们:

  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: ");
     }
    }