正如您在我的CountriesTest類中所看到的,我正在跟蹤最大的人口,地區和密度。然而,我想不出如何引用這些元素所處的位置,並使用國家類中的相應方法在該類別中插入具有最高值的國家的適當名稱。我知道界面似乎對這種情況毫無用處,但分配需要它。我在代碼中包含了筆記,我認爲我錯過的東西會去。從ArrayList調用方法的問題
編輯:這是分配的參數,因爲我的問題是有點混亂:
編寫一個程序,打印:面積最大的國家,人口最多的國家,並與該國人口密度最大。僅使用抽象類和抽象方法創建解決方案。
這裏是我的接口
public interface CountriesInterface
{
public String largestPop();
public String largestArea();
public String popDensity();
}
下面是訪問接口
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);
}
}
我已經多次閱讀你的問題,但我不明白是什麼讓你煩惱。你能澄清你的問題嗎?「但我想不出如何引用這些元素所在的位置,並使用國家類中的相應方法在所述類別中插入具有最高值的國家的適當名稱」 – 2014-10-05 20:29:13
好的我想要做的是找出哪個國家有最大的人口,例如只打印那個國家的名字 – user3451158 2014-10-05 20:42:29
我認爲首先你應該看看@Hovercraft Full Of Eels的答案,並在照顧這個問題你有 – 2014-10-05 20:43:50