gpt4 book ai didi

java - 从 ArrayList 调用方法时出现问题

转载 作者:行者123 更新时间:2023-11-30 03:44:01 25 4
gpt4 key购买 nike

正如您在“国家测试”类(class)中所看到的,我正在跟踪最大的人口、面积和密度。但是,我无法想到如何引用这些元素所在的位置,并使用 Nations 类中的相应方法来插入在所述类别中具有最高值的国家/地区的适当名称。我知道该界面对于这种情况似乎有点无用,但作业需要它。我在代码中添加了注释,我认为我丢失的东西会消失。

编辑:这是作业的参数,因为我的问题有点令人困惑:

编写一个程序,打印:面积最大的国家、人口最多的国家和人口密度最大的国家。仅使用抽象类和抽象方法创建您的解决方案。

这是我的界面

public interface CountriesInterface
{

public String largestPop();

public String largestArea();

public String popDensity();
}

这是访问该接口(interface)的类

public class Countries implements CountriesInterface
{
private String cName;
private int cPop;
private int cArea;
private int popDensity;
public Countries(String cName, int cPop, int cArea, int popDensity)
{
this.cName = cName;
this.cPop = cPop;
this.cArea = cArea;
this.popDensity = popDensity;
}
public String largestPop()
{
return cName;
}
public String largestArea()
{
return cName;
}
public String popDensity()
{
return cName;
}
}

这是我的测试类

import java.util.*;
public class CountriesTest
{
public static void main(String[] args)
{
int population = 0;
int area = 0;
int density = 0;
int arbitraryValue = 0;
int arbitraryValue2 = 0;
int arbitraryValue3 = 0;
String outputPop;
String outputArea;
String outputDensity;
ArrayList<String> countryList = new ArrayList<String>();
ArrayList<Integer> countryPopulation = new ArrayList<Integer>();
ArrayList<Integer> countryArea = new ArrayList<Integer>();
ArrayList<Integer> countryPopulationDensity = new ArrayList<Integer>();
Scanner myScanner = new Scanner(System.in);
boolean done = false;
do
{
System.out.println("1. Enter a country \n2. Print countries with the largest population, area, and population density \n3. Exit");
int choice = Integer.parseInt(myScanner.nextLine());
if (choice == 1)
{
System.out.print("Enter name of country: ");
String input1 = myScanner.nextLine();
countryList.add(input1);
System.out.print("Enter area of country in square kilometers: ");
String input2 = myScanner.nextLine();
population = Integer.parseInt(input2);
countryPopulation.add(population);
System.out.print("Enter population of country: ");
String input3 = myScanner.nextLine();
area = Integer.parseInt(input3);
countryArea.add(area);
density = population/area;
countryPopulationDensity.add(density);
Countries aCountries = new Countries(input1, population, area, density);
if(population > arbitraryValue)
{
population = arbitraryValue;
//I don't know whats supposed to go here
}
if(area > arbitraryValue2)
{
area = arbitraryValue2;
//I don't know whats supposed to go here
}
if(density > arbitraryValue3)
{
density = arbitraryValue3;
//I don't know whats supposed to go here
}
}
else if(choice == 2)
{
System.out.println("The country with the largest population: " + I don't know whats supposed to go here);
System.out.println("The country with the largest area: " + I don't know whats supposed to go here);
System.out.println("The country with the largest population density is: " + I don't know whats supposed to go here);
}
else if(choice == 3)
{
done = true;
}
else
System.out.println("Invalid Choice");
}
while (!done);
System.exit(0);
}
}

最佳答案

我担心您可能希望重新开始,因为您的代码从一开始就看起来不合时宜。每个国家/地区应该由接口(interface)方法定义,该方法返回名称 String、人口 int 或 long、面积 int 或 long 以及密度(可能是 double)。每个国家都不需要最大任何东西,并且不应该有返回 largestXXX() 的方法。因为这应该是国家/地区集合的属性,而不是单个国家/地区的属性。

为了简化,您可能需要类似的内容:

interface Country {
String getName();
int getPopulation();
// ... etc
}

class DefaultCountry implements Country {
// implement your methods
}

interface CountryCollection {
addCountry(Country country);
removeCountry(Country country);
Country getLargestPopulation();
Country getLargestArea();
....
}

好的,您的问题是实现 CountryCollection 接口(interface)的方法。具体类可能会持有 List<Country>并将其分配为 ArrayList<Country> 。然后在 getLargestXXX()方法,它使用 for 循环,遍历 ArrayList,找到具有最大 XXX 的国家并返回该国家。例如,如果您试图找到最大的人口,请在方法中创建一个 int 和 Country 局部变量,称为largestPop 和 selectedCountry,并在循环内调用 getPopulation()关于每个国家。如果人口大于当前largestPop,则将当前Country的人口设置为largestPop变量,并将当前Country设置为selectedCountry。在该方法结束时,返回选定的国家/地区。

关于java - 从 ArrayList 调用方法时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26206855/

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