繁体   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