Java 如何创建搜索并行数组的方法?
我已经写了一个工作并行数组列表,其中存储了12个国家及其相应的人口。我的第二项任务有些麻烦。我必须创建一个方法,在数组中搜索用户输入的国家/人口。(例如,如果用户输入“United Kingdom”,则相应的人口将打印出来)。然而,我不知道如何解决这个问题。 任何提示和建议将不胜感激Java 如何创建搜索并行数组的方法?,java,arrays,algorithm,Java,Arrays,Algorithm,我已经写了一个工作并行数组列表,其中存储了12个国家及其相应的人口。我的第二项任务有些麻烦。我必须创建一个方法,在数组中搜索用户输入的国家/人口。(例如,如果用户输入“United Kingdom”,则相应的人口将打印出来)。然而,我不知道如何解决这个问题。 任何提示和建议将不胜感激 import java.util.Scanner; public class InClassModule12 { public static void main(String[] args)
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();
}
}
}
import java.util.Scanner;
包含模块12的公共类
{
公共静态void main(字符串[]args)
{
字符串[]国家={“美国”、“巴西”、“中国”、“意大利”、“英国”、“西班牙”、“日本”、“德国”、“法国”、“土耳其”、“南非”、“阿根廷”};
国际[]人口={319111000、203462000、1367960000、60783711、64105654、46507760、127090000、80767000、66050000、76667864、54002000、42669500};
对于(int i=0;i
编辑:这是准确的分配:
在一个项目中,你需要存储12个国家的人口。
定义两个可并行使用的数组来存储国家及其人口的名称。
编写一个循环,使用这些数组打印每个国家的名称及其人口。
在main方法中添加一个循环,询问运行该方法的用户是否希望查找给定国家的人口。当它们指示“否”时,程序终止。
提示用户:您想查找哪个国家?
当用户输入某个内容时,程序调用countryLookup方法。
countryLookup方法接受一个参数(参数),该参数包含用户为要查找的国家输入的内容。该方法在国家名称数组中搜索名称,找到后返回人口数组中相应的人口。如果找不到国家,只需返回-1。您正在数组中查找用户提交的国家,以使用相应的索引:
for ( int i = 0; i < country.length; i++ ) {
if (country[i].equals(user_input)) {
System.out.println(country[i] + "'s population: " + population[i]);
break; // gets you out of the loop
}
}
for(inti=0;i
在变量中保存数组的长度也是一个很好的编程习惯,例如:
for ( int i = 0, l = country.length ; i < l ; i++ ) {
for(int i=0,l=country.length;i
否则数组的长度必须在每次迭代时计算。在您的示例中没有用于实际收集输入的代码,这让我相信您不熟悉如何在Java中获取用户输入。
Scanner
类是处理此问题的方便方法,您可以在此处阅读更多相关内容:
您可以使用Scanner
的nextLine()
函数获取一行用户输入
例如:
Scanner kb = new Scanner();
String userInput = kb.nextLine();
int length = country.length;
for(int i = 0; i < length; i++)
{
if(userInput.equals(country[i]))
{
System.out.println(userInput + "'s " + " population is: " + population[i]);
}
}
当然,您必须修改第一个代码示例,以便字符串的名称与方法声明中提供的名称匹配,并从第一个代码示例中删除用户输入,因为您现在在while
循环中处理它
编辑:您的程序中可能有类似的内容:
import java.util.Scanner;
public class TestClass
{
static String country[] = {"United States", "Brazil", "China", "Italy", "United Kingdom", "Spain", "Japan", "Germany", "France", "Turkey", "South Africa", "Argentina"};;
static int population[] = { 319111000, 203462000, 1367960000, 60783711, 64105654, 46507760, 127090000, 80767000, 66050000, 76667864, 54002000, 42669500};
public static void main(String args[])
{
boolean userChoice = true;
String userInput = "";
Scanner kb = new Scanner(System.in);
while(userChoice == true)
{
System.out.println("Please enter a country: ");
userInput = kb.nextLine();
searchPopulation(userInput);
//Check if the user would like to keep going here
}
kb.close();
}
public static void searchPopulation(String userInput)
{
int length = country.length;
for(int i = 0; i < length; i++)
{
if(userInput.equals(country[i]))
{
System.out.println(userInput + "'s " + " population is: " + population[i]);
}
}
}
}
import java.util.Scanner;
公共类TestClass
{
静态字符串国家[]={“美国”、“巴西”、“中国”、“意大利”、“英国”、“西班牙”、“日本”、“德国”、“法国”、“土耳其”、“南非”、“阿根廷”};;
静态整数人口[]={319111000、203462000、1367960000、60783711、64105654、46507760、127090000、80767000、66050000、76667864、54002000、42669500};
公共静态void main(字符串参数[])
{
布尔userChoice=true;
字符串userInput=“”;
扫描仪kb=新扫描仪(System.in);
while(userChoice==true)
{
System.out.println(“请输入国家:”);
userInput=kb.nextLine();
搜索人口(用户输入);
//检查用户是否希望继续访问此处
}
kb.close();
}
公共静态void searchPopulation(字符串userInput)
{
int length=country.length;
for(int i=0;i
这样,每次循环迭代时,您都会要求用户输入一个国家,然后将他们输入的国家传递给方法searchPopulation
,如果国家匹配,该方法将搜索并输出人口。尝试这段未经测试的代码,我已将数组声明为成员,实现了该方法并调用了该方法d。此外,请确保您将来能更好地将代码制表:
import java.util.Scanner;
public class InClassModule12 {
public static String[] country = {"United States", "Brazil", "China", "Italy", "United Kingdom", "Spain", "Japan", "Germany", "France", "Turkey", "South Africa", "Argentina"};
public static int[] population = { 319111000, 203462000, 1367960000, 60783711, 64105654, 46507760, 127090000, 80767000, 66050000, 76667864, 54002000, 42669500};
public static String searchPopulation(String c) {
for (int i = 0; i < country.length; i++) {
if (country[i].equals(c)) {
return population[i];
}
}
return "Not found";
}
public static void main(String[] args)
{
for ( int i = 0; i < country.length; i++ ) {
System.out.print( country[i]+ "'s population: " + " ");
System.out.print( population[i] );
System.out.println();
}
System.out.println(searchPopulation((new Scanner(System.in)).nextLine()));
}
}
import java.util.Scanner;
包含模块12的公共类{
公共静态字符串[]国家={“美国”、“巴西”、“中国”、“意大利”、“英国”、“西班牙”、“日本”、“德国”、“法国”、“土耳其”、“南非”、“阿根廷”};
公共静态整数[]人口={319111000、203462000、1367960000、60783711、64105654、46507760、127090000、80767000、66050000、76667864、54002000、42669500};
公共静态字符串搜索填充(字符串c){
对于(int i=0;iimport java.util.Scanner;
public class InClassModule12 {
public static String[] country = {"United States", "Brazil", "China", "Italy", "United Kingdom", "Spain", "Japan", "Germany", "France", "Turkey", "South Africa", "Argentina"};
public static int[] population = { 319111000, 203462000, 1367960000, 60783711, 64105654, 46507760, 127090000, 80767000, 66050000, 76667864, 54002000, 42669500};
public static String searchPopulation(String c) {
for (int i = 0; i < country.length; i++) {
if (country[i].equals(c)) {
return population[i];
}
}
return "Not found";
}
public static void main(String[] args)
{
for ( int i = 0; i < country.length; i++ ) {
System.out.print( country[i]+ "'s population: " + " ");
System.out.print( population[i] );
System.out.println();
}
System.out.println(searchPopulation((new Scanner(System.in)).nextLine()));
}
}
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;
}
}
}
public int populationForCountry(String country) {
for (int i = 0; i < countries.length; i++) {
if (countries[i].equals(country)) {
return populations[i];
}
}
return -1;
}
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);
}
}
}
}
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();
}
}
}