簡體   English   中英

無法弄清楚如何在Scanner輸入中使用getNewArrayFrom方法?

[英]Can't figure out how to use getNewArrayFrom method with Scanner input?

我不知道自己是否含糊不清,但是我的主要目標是創建一個程序,在該程序中,它要求用戶輸入一組單詞(任意給定的數量),然后告訴我哪個是第一個還是最后一個字母中最短和最長的字母。 到目前為止,我有這個:

public static void main(String[] args)
{
    Scanner Keyboard = new Scanner(System.in);

    double[] Words = getNewArrayFrom(Keyboard);
    displayList(Words);

    int choice = 1;
    while(choice > 0)
    {
        System.out.println("What would you like to do?");
        System.out.println("1) Enter a new list of up to over 9000 words.");
        System.out.println("2) Find the shortest, longest, and first and last alphabetically from the list.");
        System.out.println("0) Exit the Program");

        choice = Integer.parseInt(Keyboard.nextLine());

        if(choice < 0 || choice > 2)
        {
            System.out.println("Invalid Choice Number " + choice);
            choice = 1;
            continue;
        }
        if(choice == 1)
        {
            //Enter the Words one at a time
            System.out.println("Enter each of the words.");
            for(int i = 0; i < 9001; i++)
            {
                System.out.print((i+1) + "> ");
                Words = Keyboard.nextLine();
                Words = getNewArrayFrom(Keyboard);
            }
        }
        else if(choice == 2)
        {

        }
    }
}

}

抱歉,長度不夠,但我不知道自己在做什么。 我已經從內存中寫了所有這些,但我真的不知道該怎么辦。

我的主要觀點是試圖允許getNewArrayFrom()方法根據輸入字符串(如果選擇)創建一個數組。 如果您至少可以指出如何使我打印出最短,最長,第一個和最后一個字母單詞的方式或方向,那么我將永遠是偉大的!

對不起,英語不是我的母語。

由於這是偽作業,因此我將省略細節。

我將承擔所有的用戶輸入將是一個線,例如,他們提供了一個輸入字符串,如apple banana orange grape 我還要假設每個單詞都用空格分隔。

我將使用Scanner讀取單行輸入,查看nextLine() 然后,我將掃描程序返回的行用空格分開,您可以使用循環和indexOf(...)substring(...)手動執行此操作,並將其存儲在您創建的數組中,或者我們可以使用split()具有內置的Java String函數可以為我們處理該部分。

如果使用some_str.split(...) ,它將自動為我們提供一個String []數組,因此您可以根據需要返回它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM