我正在使用while循环创建一个数组。 (由于我以这种方式创建数组的原因,请转到https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt)虽然我的数组(data
)是在while循环内创建的,但我无法在while循环之外访问它。我希望这样做,以便用户可以输入一个国家的名称,比如印度,并获得该国家的移动用户数量。
String address = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt";
URL pageLocation = new URL(address);
Scanner in1 = new Scanner(pageLocation.openStream());
Scanner in = new Scanner(System.in);
String line;
System.out.print("Please enter the name of the country you would like to see the mobile users for: ");
String country = in.next();
while (in1.hasNextLine()){
line = in1.nextLine();
String[] data = line.split("\t");
if (data[1].contains(country) == true){
System.out.println("Country name: " + data[1]);
System.out.println("Mobile phone subscribers: " + data[2]);
return;
}
else{
System.out.println("No country found with that name!");
return;
}
}
如果输入在循环内部,则输入有效,但仅适用于中国,因为它是列表中的第一个国家/地区。我理解为什么它不能正常工作,以为我不确定如何修复它除了将if
语句放在循环之外,但如果我这样做,语句就无法到达我的数组。有什么建议吗?
答案 0 :(得分:3)
问题在于:
if (data[1].contains(country) == true){
System.out.println("Country name: " + data[1]);
System.out.println("Mobile phone subscribers: " + data[2]);
return;
} else {
System.out.println("No country found with that name!");
return; //<-----ISSUE
}
在else子句中调用return
时,它会终止程序。它真正需要做的是迭代循环的第二次运行。
删除return
中的else-statment
。
以下是修订后的代码:
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) throws IOException {
String address = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt";
URL pageLocation = new URL(address);
Scanner in1 = new Scanner(pageLocation.openStream());
Scanner in = new Scanner(System.in);
String line;
System.out
.print("Please enter the name of the country you would like to see the mobile users for: ");
String country = in.next();
while (in1.hasNextLine()) {
line = in1.nextLine();
String[] data = line.split("\t");
if (data[1].contains(country) == true) {
System.out.println("Country name: " + data[1]);
System.out.println("Mobile phone subscribers: " + data[2]);
return; //<--- will exit after printing ^
}
}
System.out.println("No country found with that name!");
}
}
以下是一个示例运行:{input} India
Please enter the name of the country you would like to see the mobile users for: India
Country name: India
Mobile phone subscribers: 893,862,000
答案 1 :(得分:1)
您无法迭代到第二行,因为您在第一次迭代后返回,无论是否找到该国家/地区。
我建议从else条件中删除return
语句。
我还使用了boolean
找到的变量,该变量将在找到国家/地区后设置,只有当该国家/地区不在列表中时才会显示No country found
消息。
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class CountryName {
public static void main(final String[] args) throws IOException {
final String address = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt";
final URL pageLocation = new URL(address);
final Scanner in1 = new Scanner(pageLocation.openStream());
final Scanner in = new Scanner(System.in);
boolean found = false;
String line;
System.out
.print("Please enter the name of the country you would like to see the mobile users for: ");
final String country = in.next();
while (in1.hasNextLine()) {
line = in1.nextLine();
final String[] data = line.split("\t");
if (data[1].contains(country) == true) {
System.out.println("Country name: " + data[1]);
System.out.println("Mobile phone subscribers: " + data[2]);
found = true;
return;
}
}
if (!found) {
System.out.println("No Country Found");
}
in.close();
in1.close();
}
}
另一方面,如果您想使用集合,您的程序将变得更简洁易读。这与HashMap
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class CountryName {
public static void main(final String[] args) throws IOException {
final String address = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt";
final URL pageLocation = new URL(address);
final Scanner in1 = new Scanner(pageLocation.openStream());
final Scanner in = new Scanner(System.in);
final Map<String, String> countryMap = new HashMap<String, String>();
while (in1.hasNextLine()) {
final String[] line = in1.nextLine().split("\t");
countryMap.put(line[1], line[2]);
}
System.out.print("Please enter the name of the country you would like to see the mobile users for: ");
final String country = in.next();
if (countryMap.containsKey(country)) {
System.out.println("Country Name: " + country);
System.out.println("Mobile phone subscribers: "+ countryMap.get(country));
} else {
System.out.println("No Country found with that name");
}
in.close();
in1.close();
}
}
答案 2 :(得分:0)
尝试放
String[] data;
在循环之前。这将使其范围大于循环。
答案 3 :(得分:0)
在&#34;之外宣布数据,而#34;但是把它分配到里面。
String address = "https://www.cia.gov/library/publications/the-world- factbook/rankorder/rawdata_2151.txt";
URL pageLocation = new URL(address);
Scanner in1 = new Scanner(pageLocation.openStream());
Scanner in = new Scanner(System.in);
String line;
System.out.print("Please enter the name of the country you would like to see the mobile users for: ");
String country = in.next();
String[] data;
while (in1.hasNextLine()){
line = in1.nextLine();
data = line.split("\t");
if (data[1].contains(country) == true){
System.out.println("Country name: " + data[1]);
System.out.println("Mobile phone subscribers: " + data[2]);
return;
} else{
System.out.println("No country found with that name!");
return;
}
}
Objects.toString(data); // now its visible