第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java - 如何在 switch 語句中調用方法?

Java - 如何在 switch 語句中調用方法?

繁花不似錦 2023-09-20 19:11:41
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);
查看完整描述

4 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

您粘貼的代碼存在一些問題 - 其中一些問題已在之前的評論/答案中確定,但為了完整性,我也會在此處列出它們:

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

 }

}


查看完整回答
反對 回復 2023-09-20
?
慕姐4208626

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


    }

}


查看完整回答
反對 回復 2023-09-20
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

從顯示的代碼中,您可以不帶參數調用“averageLength()”,而它需要兩個參數:單詞數組及其計數。

該調用還包含一個拼寫錯誤(缺少“g”)。

因此,編譯器無法找到該函數,因為它引用了實際不存在的函數。

此外,在“averageLength()”的兩個參數中,單詞數組未使用,您可以重新掃描單詞,而不是使用通過其他開關情況建立的列表。這很可能是一個邏輯錯誤。


查看完整回答
反對 回復 2023-09-20
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

請修復:

  1. 在方法中main()averageLenth();averageLength(words, count);

  2. 在方法中averageLength()double sum;double sum = 0;


查看完整回答
反對 回復 2023-09-20
  • 4 回答
  • 0 關注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號