簡體   English   中英

如何獲取用戶輸入並在不同類中的數組列表中搜索用戶輸入?

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

更新:應用@ user27158建議的改進(謝謝)后,當我運行程序時遇到了另一個問題。

彈出錯誤,從本質上使程序停止運行。 在調查了這一點之后,我無法弄清問題所在。 再一次,我是編程新手,很可能我只是想念一些完全簡單的東西。

錯誤信息:

線程“主”中的異常java.lang.UnsupportedOperationException:不支持。 在country.game.Main.main(Main.java:36)的country.game.EuropeanCountries.EuropeanCountriesList(EuropeanCountries.java:17)處返回Java:1 BUILD FAILED(總時間:12秒)

錯誤發生在以下行:

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

主類:

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


“歐洲”類

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

}

任何幫助將不勝感激!

首先,您應該對代碼進行一些改進。

  1. 運行永遠不會增加,因此while循環永遠不會結束
  2. 您只想初始化掃描儀一次,因此可以在循環外進行
  3. 您正在嘗試將String與int進行比較
  4. 您的列表“ UsedCountries”是通用的。 這絕不是一個好主意,因為它不限制可以輸入的類型,這意味着您可能會出現僅在運行時顯示的錯誤。 應該聲明為List<String> UsedCountries = new ArrayList<>();
  5. 變量和方法名稱應以小寫字母https://www.oracle.com/technetwork/java/codeconventions-135099.html開頭

解決方法:如何從其他類訪問arraylist

  • 從你的方法返回
  • 在返回的列表上工作
    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;
    }

然后

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

上面的代碼允許您在當前代碼中使用arraylist。 我會建議一些進一步的改進

  1. 獲得列表后,只需使用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. 不必在每次調用該方法時都創建國家列表,只需聲明一次並返回存儲的列表即可
public class Europe {
    private static final List<String> EuropeanCountries = Arrays.asList(
        new String[]{
            "Albania", 
            "Andorra",
            ...
        }
    );

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

希望這對你有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM