【问题标题】:Taking user input and adding it to an array接受用户输入并将其添加到数组中
【发布时间】:2012-12-23 17:33:33
【问题描述】:

所以我正在重新学习 java 并且已经有一段时间了。我正在尝试构建一个基本程序(在代码 cmets 中进行了解释),但我无法记住如何获取用户输入并将其添加到数组中。我很难记住如何遍历用户输入并测试他们是否输入了任何内容,以及如果他们确实输入了某些内容,则将输入附加到数组中。

//This program will ask user for for there favorite four games
//If the answer is blank, it will ask again for a game title
//The program will than store there answers into an array
//The program will than display the array in random order
//it will then give the amount of games in the array with an integer



import java.util.*;

public class MultipleClassesMain {


public static void main(String[] args) {

    //Array holds 4 string inputs from user
    String gameArray[] = new String[4];

    //importing scanner element-------
    Scanner input = new Scanner(System.in);

    //Introduction---------------
    System.out.println("Hey there!!!");
    System.out.println("Please tell us four game titles you like to play!!!");

    //Asks what game user likes and takes user input into a variable
    System.out.println("So what a game you like?: ");
    String temp = input.nextLine();

    //This loop will test against blank user input
    while (temp.equals("") || (temp.equals("   ")){
        System.out.println("Your game can't be blank.  Enter again: ");

        }

    }

}

这是我到目前为止的代码。如果有人能给我一些建设性的批评和一些关于如何循环用户输入(测试输入)并将输入附加到数组的指示,我将不胜感激。

干杯

【问题讨论】:

    标签: java arrays while-loop user-input


    【解决方案1】:

    首先:使用List 代替数组进行用户输入。只需.add() 您的输入即可。但请参阅下文以获得更好的解决方案,即使用Set

    第二个:String 有一个 .trim() 方法,它可以删除开头和结尾的空格,使用它并使用 .isEmpty() 测试空字符串。

    第三:List 不检测重复条目,但 Set 可以,前提是其条目正确实现 equals()hashCode()String 可以,因此以下代码说明了这一点( Set.add() 方法当且仅当集合因操作而被修改时才返回 true。

    示例代码:

    public static void main(final String... args)
    {
        // Set of valid user inputs
        final Set<String> gameList = new HashSet<String>();
        // Object from which user inputs are read
        final Scanner in = new Scanner(System.in);
    
        // Introduction
        System.out.println("Hey there!!");
        System.out.println("Please tell us four game titles you like to play!!!");
    
        // What the user enters
        String input;
    
        // Check that 4 titles have been entered, don't get out of the loop until then
        while (gameList.size() < 4) {
            System.out.print("Enter the name of a game: ");
            // Read one input, trim all beginning and trailing whitespaces
            input = in.nextLine().trim();
            // If the resulting string is empty, input is invalid: ask for another
            if (input.isEmpty()) {
                System.out.println("Empty inputs not accepted!");
                continue;
            }
            if (!gameList.add(input))
                System.out.println("You have already selected this game (" + input + ')');
        }
    
        // Print out the list of inputs
        System.out.println("The list of selected games is: " + gameList);
    
    }
    

    【讨论】:

    • 如果你不介意的话,你能给我举个例子来说明如何使用它吗?
    • 查看编辑。然而,还有一件事要添加:重复条目检测。已添加。
    • 非常感谢。感谢您对代码发表评论,我现在更好地理解了它。
    【解决方案2】:
    for (int i = 0; i < 4; i++) {
            String temp = input.nextLine();
            if (temp.equals("") || (temp.equals("   "))) {
                System.out.println("Your game can't be blank.  Enter again: ");
                i--;
            } else
                gameArray[i] = temp;
    
        }
    

    试试这个。这就是你要的……是吗?

    【讨论】:

    • 我相信是的。我忘记了我必须遍历列表/数组以确保每个点都填充有输入。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多