【问题标题】:How to check user input from an ArrayList multiple times in java如何在java中多次检查来自ArrayList的用户输入
【发布时间】:2021-12-14 23:52:20
【问题描述】:

在我的代码中,ArrayList 将打印到控制台。用户将从那里输入列表中的名称。它将检查列表中的名称。我让我的代码分别检查用户输入 3 次。我遇到的问题是,有时它会正确地通过代码,直到您输入错误的值。然后它就到了最后,它说“我在一切之外”。我只用它来查看我的代码在哪里。我想知道我的代码哪里出错了。我怎样才能解决这个问题?非常感谢任何帮助!

        ArrayList<String> forwards = new ArrayList<String>();
    
        forwards.add("Matthew Barzal");
        forwards.add("Josh Bailey");
        forwards.add("Anthony Beauvillier");
        forwards.add("Kieffer Bellows");
        forwards.add("Casey Cizikas");
        forwards.add("Cal Clutterbuck");
        forwards.add("Anders Lee");
        forwards.add("Matt Martin");
        forwards.add("Brock Nelson");
        forwards.add("Oliver Wahlstrom");
        forwards.add("Zach Parise");
        forwards.add("Kyle Palmieri");
        forwards.add("JG Pageau");

    
    System.out.println("These are your starting forwards:");
    for (int i = 0; i < forwards.size(); i++) {
        System.out.println(forwards.get(i));
        }
    System.out.println();
    
    System.out.println("Please pick three(3) forwards to be line in #1:");
    Scanner input = new Scanner(System.in);
    String userInput;
    String userInput2 = "";
    String userInput3 = "";

userInput = input.nextLine();
    
    
    while (true) {
        
        for(String i : forwards) {
            if(userInput.equals(i)) {
                System.out.println("Please pick another forward...");
                System.out.println("I am in first if statement");
                userInput2 = input.nextLine();
                continue;
               // return;
                } else if (userInput2.equals(i)) {
                    System.out.println(userInput);
                    System.out.println("Please pick another forward...");
                    System.out.println("I am in second if statement");
                    userInput3 = input.nextLine();
                    //continue;
                
                } else if (userInput3.equals(i)) {
                
                        System.out.println("I am in third if statement");
                        System.out.println("Your forwards for Line # 1 are: " + 
                        userInput + " " + userInput2 + " " + userInput3);
                        break;
                        //return;
                        
                    } 
        
        }
    
        System.out.println("I am outside everything");
        System.out.print("Input not found in forwards list. Enter new value: \n");
        userInput = input.nextLine();
        continue;
    }

【问题讨论】:

  • 我知道你是纽约岛民的粉丝 :) 你能澄清一下你所说的价值不正确是什么意思吗?您是说如果您输入的名称不在forwards 中,代码会跳转到末尾?
  • 是的,我是岛民的忠实粉丝!!!所以基本上,用户使用扫描器从 ArrayList 中输入一个名称。如果名称不匹配,那么它应该跳到最后给出“未找到输入错误”。但它应该回到开头让用户输入另一个名字。有时您输入了正确的名称,因此它转到第二个 if 语句,但正确输入了第二个名称,它会跳回“未找到输入错误”。甚至认为名字是正确的。身份证
  • 我可以通过输入第二个名称来重现您所描述的问题,该名称在列表中早于第一个名称。例如,如果您选择 Zach Parise 然后选择 Anders Lee,您会遇到这个问题,但反过来也可以。问题是您的循环已前进到第一个玩家,因此它将无法找到列表中较早出现的任何后续玩家。
  • 泰勒,我的兄弟,你中了大奖。这正是我希望应用程序工作的方式。太感谢了!!那么你在纽约吗?你也是岛民的粉丝吗?
  • 而且,通过查看代码,我非常了解您所做的事情。您基本上将用户的每个条目添加到名为 selectedForwards 的新列表中,并从名为 forwards 的原始 Set 列表中删除该条目。通过使用以下条件 (selectedForwards.size()

标签: java loops arraylist input


【解决方案1】:

我使用集合重构了您的代码,使其更短更简单。看看这个,看看它是否满足您的需求。它现在应该可以正常工作了。

        Set<String> forwards = new HashSet<>();
    
        forwards.add("Matthew Barzal");
        forwards.add("Josh Bailey");
        forwards.add("Anthony Beauvillier");
        forwards.add("Kieffer Bellows");
        forwards.add("Casey Cizikas");
        forwards.add("Cal Clutterbuck");
        forwards.add("Anders Lee");
        forwards.add("Matt Martin");
        forwards.add("Brock Nelson");
        forwards.add("Oliver Wahlstrom");
        forwards.add("Zach Parise");
        forwards.add("Kyle Palmieri");
        forwards.add("JG Pageau");

    
        System.out.println("These are your starting forwards:");
        for (String forward : forwards) {
            System.out.println(forward);
        }
        
        System.out.println("Please pick three(3) forwards to be line in #1:");
        Scanner input = new Scanner(System.in);
        List<String> selectedForwards = new ArrayList<>();
        String userInput;
    
        while (selectedForwards.size() < 3) {
            System.out.println("Select the next forward: \n");
            userInput = input.nextLine();
            if (!forwards.contains(userInput)) {
                System.out.println("Input not found in forwards list. Enter new value: \n");
                continue;
            }
            selectedForwards.add(userInput);
            forwards.remove(userInput);
        }
        
        System.out.println(selectedForwards);
    
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 2022-01-20
    • 1970-01-01
    • 2017-12-24
    • 2017-07-27
    相关资源
    最近更新 更多