简体   繁体   English

如何获取用户输入并在不同类中的数组列表中搜索用户输入?

[英]How do I take a user input and search through an arraylist in a different class for the user input?

Update: After applying improvements suggested by @user27158 (thank you) i have come across another issue when i come to run the program. 更新:应用@ user27158建议的改进(谢谢)后,当我运行程序时遇到了另一个问题。

An error pops up and essentially stops the program from continuing. 弹出错误,从本质上使程序停止运行。 After looking into this, I cannot figure out what the issue is. 在调查了这一点之后,我无法弄清问题所在。 Once again, i am new to programming and it is likely that i am just missing something completely simple. 再一次,我是编程新手,很可能我只是想念一些完全简单的东西。

Error Message: 错误信息:

Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet. 线程“主”中的异常java.lang.UnsupportedOperationException:不支持。 at country.game.EuropeanCountries.EuropeanCountriesList(EuropeanCountries.java:17) at country.game.Main.main(Main.java:36) Java returned: 1 BUILD FAILED (total time: 12 seconds) 在country.game.Main.main(Main.java:36)的country.game.EuropeanCountries.EuropeanCountriesList(EuropeanCountries.java:17)处返回Java:1 BUILD FAILED(总时间:12秒)

The Error occurs at the line: 错误发生在以下行:

List<String> euCountries = EuropeanCountries.EuropeanCountriesList();

Main Class: 主类:

package country.game;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Main {

    public static List<String> EuropeanCountriesList() {
        List<String> EuropeanCountries = new ArrayList<>();
        return EuropeanCountries;
    }



    public static void main(String[] args) {

        boolean Running = true;

        List<String> UsedCountries = new ArrayList<>();

        Scanner Reader = new Scanner(System.in);

        while(Running == true){



            System.out.println("Pick a Country: ");
            String UserChoice = Reader.next();




            List<String> euCountries = EuropeanCountries.EuropeanCountriesList();

            if(!euCountries.contains(UserChoice)){
                System.out.println("That is not a valid country");
            } else {
                if(UsedCountries.contains(UserChoice)) {
                    System.out.println("Sorry you have already used that country");
                } else {
                    System.out.println("Correct! That Country is in Europe!");
                    UsedCountries.add(UserChoice);
                }
            }
        }        
    }           
}


"Europe" class “欧洲”类

package country.game;

import java.util.Arrays;
import java.util.List;

public class Europe {

    private static final List<String> EuropeanCountries = Arrays.asList(
            new String[]{
                "Albania",
                "Andorra",
                "Austria",

                "Belarus",
                "Belgium",
                "Bosnia and Herzegovina",
                "Bulgaria",

                "Croatia",
                "Czechia",

                "Denmark",

                "England",
                "Estonia",

                "Finland",
                "France",

                "Germany",
                "Greece",

                "Hungary",

                "Iceland",
                "Ireland",               
                "Italy",

                "Kosovo",

                "Latvia",
                "Liechtenstein",
                "Lithuania",
                "Luxembourg",

                "Malta",
                "Moldova",
                "Monaco",
                "Montenegro",

                "Netherlands",
                "Northern Ireland",
                "North Macedonia",
                "Norway",

                "Poland",
                "Portugal",

                "Romania",

                "San Marino",
                "Scotland",
                "Serbia",
                "Slovakia",
                "Slovenia",
                "Spain",
                "Sweden",
                "Switzerland",

                "Turkey",

                "Ukraine",

                "Vatican City",

                "West Russia",
            }
    );


    public static List<String> EuropeanCountriesList(){

    return EuropeanCountries;
    }    

}

Any Help would be greatly appreciated! 任何帮助将不胜感激!

To start with, there a few a few improvements that you should make in the code; 首先,您应该对代码进行一些改进。

  1. Running is never incremented so your while loop will never end 运行永远不会增加,因此while循环永远不会结束
  2. You only want to initialise the scanner once, so do it outside of the loop 您只想初始化扫描仪一次,因此可以在循环外进行
  3. You are trying to compare a String to an int 您正在尝试将String与int进行比较
  4. Your list 'UsedCountries' is generic. 您的列表“ UsedCountries”是通用的。 That's never a good idea as it doesn't restrict which types you can put into them which means you may have errors that only show up at runtime. 这绝不是一个好主意,因为它不限制可以输入的类型,这意味着您可能会出现仅在运行时显示的错误。 It should be declared as List<String> UsedCountries = new ArrayList<>(); 应该声明为List<String> UsedCountries = new ArrayList<>();
  5. Variable and method names should start with lowercase https://www.oracle.com/technetwork/java/codeconventions-135099.html 变量和方法名称应以小写字母https://www.oracle.com/technetwork/java/codeconventions-135099.html开头

The fix: How to access the arraylist from your other class 解决方法:如何从其他类访问arraylist

  • return it from your method 从你的方法返回
  • work on the returned list 在返回的列表上工作
    public static List<String> EuropeanCountriesList() {
        // declaring as List as it is best to not tie yourself to a specific implementation
        List<String> EuropeanCountries = new ArrayList<>();
        ...
        return EuropeanCountries;
    }

then 然后

    String UserChoice = Reader.next()
    List<String> euCountries = EuropeanCountries.EuropeanCountriesList();
    ...

The above code allows you to use the arraylist in your current code. 上面的代码允许您在当前代码中使用arraylist。 I would suggest a few further improvements though 我会建议一些进一步的改进

  1. Once you have your list, just use the contains method instead of the for loops 获得列表后,只需使用contains方法而不是for循环
    List<String> euCountries = EuropeanCountries.EuropeanCountriesList();
    // step 1 - check if it is one of the acceptable countries
    if (!euCountries.contains(UserChoice)) {
        System.out.println("That is not a valid country");
    } else {
        // step 2 - check if it has already been selected
        if (UsedCountries.contains(UserChoice)) {
            System.out.println("Sorry You Have Already Used That Country");
        } else {
            System.out.println("Correct! That Country is in Europe!");
            UsedCountries.add(UserChoice);
        }
    }
  1. Instead of creating your list of countries every time that method is called, declare it once and return the stored list instead 不必在每次调用该方法时都创建国家列表,只需声明一次并返回存储的列表即可
public class Europe {
    private static final List<String> EuropeanCountries = Arrays.asList(
        new String[]{
            "Albania", 
            "Andorra",
            ...
        }
    );

    public static List<String> EuropeanCountriesList() {
        return EuropeanCountries;
    }
}

Hope this all helps 希望这对你有帮助

暂无
暂无

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

相关问题 我将如何接受用户输入并将其添加到arraylist中? - How would I take user input and add it into an arraylist? 如何获取用户输入并将其成功存储在 ArrayList 中? 那么我如何让我的程序向我显示 ArrayList 中的所有元素? - How do i take user input and store it successfully in an ArrayList? Then how do i get my program to show me all the elements in the ArrayList? 我想通过用户输入添加到arrayList - I want to add to an arrayList through user input 如何从一个 class 中的扫描仪获取用户输入,然后将其应用于同一 package 中的另一个 class? - How do I take an user input from Scanner in one class and then apply it to another class in the same package? 如何将数据输入到用户定义类的arraylist中? - how to input data into an arraylist for user defined class? 如何在Java中将类型为String的用户输入添加到ArrayList? - How do I add a User Input of type String to an ArrayList in Java? 如何获取用户输入并将其存储到另一个类中的数组中? || 爪哇 - How do I take user input and store it into an array that is in another class? || Java 如何获取用户输入并将其显示在阵列上? - How do I take user input and show it on my array? 接受用户输入并搜索二维数组并打印结果 - Take user input and search through 2D array and printout the result 我无法弄清楚如何将用户输入放在逗号分隔的列表中并将其放入ArrayList中 - I cannot figure out how to take user input in a comma-separated list and place it into an ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM