[英]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;
}
}
任何帮助将不胜感激!
首先,您应该对代码进行一些改进。
List<String> UsedCountries = new ArrayList<>();
解决方法:如何从其他类访问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。 我会建议一些进一步的改进
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;
}
}
希望这对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.