首页 > 解决方案 > 如何在java中多次检查来自ArrayList的用户输入

问题描述

在我的代码中,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;
    }

标签: javaloopsarraylistinput

解决方案


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

        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);
    
        }

推荐阅读