简体   繁体   English

如何在java中多次检查来自ArrayList的用户输入

[英]How to check user input from an ArrayList multiple times in java

In my code, the ArrayList will print to the console.在我的代码中,ArrayList 将打印到控制台。 From there the user will input a name from the list.从那里,用户将从列表中输入一个名称。 It will check the name to the list.它将检查名称到列表中。 I have my code checking user input 3 separate times.我的代码检查用户输入 3 次。 The issue I'm having is that sometimes it goes thru the code correctly until you enter an incorrect value.我遇到的问题是有时它会正确地通过代码,直到您输入不正确的值。 Then it just goes to the end where it says "I'm outside everything".然后它就走到最后,它说“我在一切之外”。 I only use that to see where my code is.我只用它来查看我的代码在哪里。 I'm wondering where my code is going wrong.我想知道我的代码哪里出错了。 How can I fix this?我怎样才能解决这个问题? Any assistance is greatly appreciated!非常感谢任何帮助!

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

I've refactored your code to be shorter and simpler, using a set.我使用 set 重构了您的代码,使其更短更简单。 Take a look at this and see if it meets your needs.看看这个,看看它是否满足您的需求。 It should be working correctly now.现在应该可以正常工作了。

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

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

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