gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:30:04 25 4
gpt4 key购买 nike

更新:在应用@user27158建议的改进(谢谢)之后,当我运行该程序时,我遇到了另一个问题。

弹出错误并基本上阻止程序继续运行。经过研究后,我无法弄清楚问题出在哪里。再说一次,我是编程新手,很可能我只是错过了一些完全简单的东西。

错误消息:

线程“main”中出现异常 java.lang.UnsupportedOperationException:尚不支持。 在country.game.EuropeanCountries.EuropeanCountriesList(EuropeanCountries.java:17) 在country.game.Main.main(Main.java:36) Java 返回:1构建失败(总时间: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

修复:如何从其他类访问数组列表

  • 从您的方法返回它
  • 处理返回的列表
    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();
...

上面的代码允许您在当前代码中使用数组列表。不过,我会建议一些进一步的改进

  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);
}
}
  • 不必在每次调用该方法时都创建国家/地区列表,而是声明一次并返回存储的列表
  • public class Europe {
    private static final List<String> EuropeanCountries = Arrays.asList(
    new String[]{
    "Albania",
    "Andorra",
    ...
    }
    );

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

    希望这一切有帮助

    关于java - 如何获取用户输入并在不同类中的数组列表中搜索用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56890365/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com