简体   繁体   English

无法弄清楚如何在Scanner输入中使用getNewArrayFrom方法?

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

I don't know if I'm being vague or not, but what my main goal is is to create a program in which it asks the user for a set of words (any given amount) and then tells me which is first and last in the alphabet and which is shortest and longest. 我不知道自己是否含糊不清,但是我的主要目标是创建一个程序,在该程序中,它要求用户输入一组单词(任意给定的数量),然后告诉我哪个是第一个还是最后一个字母中最短和最长的字母。 So far I have this: 到目前为止,我有这个:

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

        }
    }
}

} }

Sorry for the length but I have no idea what I'm doing. 抱歉,长度不够,但我不知道自己在做什么。 I've written this all from memory and I don't really know what to do. 我已经从内存中写了所有这些,但我真的不知道该怎么办。

My main point is just trying to allow the getNewArrayFrom() method to create an array from the input strings if chosen. 我的主要观点是试图允许getNewArrayFrom()方法根据输入字符串(如果选择)创建一个数组。 If you can at least point me in the way or direction of how to get it to print the shortest and longest and first and last alphabetical words then I would be forever greatful! 如果您至少可以指出如何使我打印出最短,最长,第一个和最后一个字母单词的方式或方向,那么我将永远是伟大的!

Sorry for the English it is not my first language. 对不起,英语不是我的母语。

Since this is pseudo-homework, I'll leave out details. 由于这是伪作业,因此我将省略细节。

I am going to assume that all the user input will be from one line, ie., they provide an input string such as apple banana orange grape . 我将承担所有的用户输入将是一个线,例如,他们提供了一个输入字符串,如apple banana orange grape I am also going to assume each word is separated by spaces. 我还要假设每个单词都用空格分隔。

I would used the Scanner to read a single line of input, look into nextLine() . 我将使用Scanner读取单行输入,查看nextLine() Then I would split the line that came back from the scanner by spaces, you can do this manually with a loop and indexOf(...) and substring(...) and store it in an array you created, or we can use the split() to have the built-in Java String functions handle that part for us. 然后,我将扫描程序返回的行用空格分开,您可以使用循环和indexOf(...)substring(...)手动执行此操作,并将其存储在您创建的数组中,或者我们可以使用split()具有内置的Java String函数可以为我们处理该部分。

If you use some_str.split(...) , it will give us a String[] array automaticaly, so you can return it if you like. 如果使用some_str.split(...) ,它将自动为我们提供一个String []数组,因此您可以根据需要返回它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM