我编写了一个工作并行数组列表,其中存储了 12 个国家及其相应的人口。我在完成第二项作业时遇到了一些麻烦。我必须创建一个方法来在数组中搜索用户输入的国家/人口。 (例如,如果用户输入“英国”,则会打印相应的人口)。但是,我不确定如何解决这个问题。 任何提示和建议将不胜感激!
import java.util.Scanner;
public class InClassModule12
{
public static void main(String[] args)
{
String[] country = {"United States", "Brazil", "China", "Italy", "United Kingdom", "Spain", "Japan", "Germany", "France", "Turkey", "South Africa", "Argentina"};
int[] population = { 319111000, 203462000, 1367960000, 60783711, 64105654, 46507760, 127090000, 80767000, 66050000, 76667864, 54002000, 42669500};
for ( int i = 0; i < country.length; i++ )
{
System.out.print( country[i]+ "'s population: " + " ");
System.out.print( population[i] );
System.out.println();
}
}
}
已编辑:这是确切的分配: 在一个程序中,您需要存储 12 个国家/地区的人口。 定义两个可以并行使用的数组来存储国家名称及其人口。 编写一个循环,使用这些数组打印每个国家的名称及其人口。 在您的 main 方法中添加一个循环,询问运行它的用户是否要查找给定国家/地区的人口。当他们表示“否”时,程序终止。 提示用户:您想查找哪个国家/地区? 用户输入内容后,程序会调用 countryLookup 方法。 countryLookup 方法接受一个参数(参数),其中包含用户输入的要查找的国家/地区的内容。该方法在国家/地区名称数组中搜索名称,找到后返回人口数组中的相应人口。如果它没有找到国家,只需返回 -1。
最佳答案
关于这个问题,我有两个想法要分享。首先是它大声疾呼要以面向对象的方式进行编程。也就是说,有一些数据集合,以及一些将对这些数据进行操作的方法。因此,拥有一个其对象实际包含该数据并且其方法进行处理的类是有意义的;例如,与一切都是静态的、没有任何东西被实例化的程序相反。 main
方法可以处理与用户的交互。尽管您可以将 main
放入存储数据的类中,但最好不要这样做。
我的第二个想法是,对于实际搜索数组,使用 Apache commons ArrayUtils
类是有意义的,它有方法 indexOf
,非常适合这个用例。但是,我已经提出了一个替代方案,以防 Gianna 的老师反对使用外部库。老实说,我觉得优秀的 Java 程序员和平庸的 Java 程序员之间的主要区别之一是了解哪些类是现成的,用于执行某些常见任务。
无论如何,存储数据的类可能如下所示。
import org.apache.commons.lang3.ArrayUtils;
public class PopulationLookup {
private String[] countries;
private int[] populations;
public PopulationLookup(String[] countries, int[] populations) {
this.countries = countries;
this.populations = populations;
}
public void printAll() {
for (int i = 0; i < countries.length; i++) {
System.out.format(
"Country: %s, population %d%n", countries[i], populations[i]);
}
}
public int populationForCountry(String country) {
int entry = ArrayUtils.indexOf(countries, country);
if (entry != -1) {
return populations[entry];
} else {
return -1;
}
}
}
这并不完美。如果两个数组的大小不同,或者其中一个数组为空,或者其他一些可能出错的事情,则不会发生错误处理。此外,理想情况下,您将复制所有数组条目,而不仅仅是对数组本身的引用,以防调用此数组的类随后开始修改数组条目。这些都是在版本 2 中进行的良好更改。
但是这个类演示了如何将数据包装在一个对象中,如何通过构造函数传递数据,当然还有搜索。
您需要将 Apache commons lang3 添加到构建路径才能使其正常工作。这是 documented here JAR 是 available from here .如果您真的不想使用外部库,可以像这样更改 populationForCountry
方法。
public int populationForCountry(String country) {
for (int i = 0; i < countries.length; i++) {
if (countries[i].equals(country)) {
return populations[i];
}
}
return -1;
}
然后,要完成分配,您需要包含main
的类来调用它。需要注意的重要一点是 PopulationLookup
类的实例化,传递您提供的数据。
import java.util.Scanner;
public class InClassModule12 {
public static void main(String[] args)
{
String[] countries = {
"United States", "Brazil", "China", "Italy", "United Kingdom", "Spain",
"Japan", "Germany", "France", "Turkey", "South Africa", "Argentina"};
int[] populations = {
319111000, 203462000, 1367960000, 60783711, 64105654, 46507760,
127090000, 80767000, 66050000, 76667864, 54002000, 42669500};
try( Scanner input = new Scanner(System.in)) {
PopulationLookup lookup = new PopulationLookup(countries, populations);
lookup.printAll();
while(true) {
System.out.println("Would you like to look up a country?");
String response = input.nextLine();
if (response.equalsIgnoreCase("no")) {
System.out.println("Good bye");
break;
}
System.out.println("What country would you like to look up?");
String country = input.nextLine();
int population = lookup.populationForCountry(country);
System.out.format("The population of %s is %d%n", country, population);
}
}
}
}
编辑
此代码使用“尝试使用资源”功能来确保 Scanner
最后关闭。这是在 Java 7 中引入的。如果您使用的是早期版本的 Java,那么最好使用 finally
block 来关闭 Scanner
。看起来像这样。上面的 Java 7 代码基本上是编写相同内容的更短方式。
public class InClassModule12 {
public static void main(String[] args)
{
String[] countries = {
"United States", "Brazil", "China", "Italy", "United Kingdom", "Spain",
"Japan", "Germany", "France", "Turkey", "South Africa", "Argentina"};
int[] populations = {
319111000, 203462000, 1367960000, 60783711, 64105654, 46507760,
127090000, 80767000, 66050000, 76667864, 54002000, 42669500};
Scanner input = new Scanner(System.in);
try {
PopulationLookup lookup = new PopulationLookup(countries, populations);
lookup.printAll();
while(true) {
System.out.println("Would you like to look up a country?");
String response = input.nextLine();
if (response.equalsIgnoreCase("no")) {
System.out.println("Good bye");
break;
}
System.out.println("What country would you like to look up?");
String country = input.nextLine();
int population = lookup.populationForCountry(country);
System.out.format("The population of %s is %d%n", country, population);
}
} finally {
input.close();
}
}
}
关于java - 如何制作搜索并行数组的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27028033/