有可能做这样的事吗?假设我想在CountriesTest中添加我赋予值的值,并将它们添加到Countries中的ArrayList。另外我如何引用aCountries打印选项2,看到我在选项1中创建它我无法在其他地方访问它。
这是我的界面
public interface CountriesInterface
{
public String largestPop();
public String largestArea();
public String popDensity();
}
这是国家类
import java.util.*;
public class Countries implements CountriesInterface
{
private final List<CountriesInterface> theCountries = new ArrayList<>();
private String cName;
private String finalPopName;
private String finalAreaName;
private String finalDensityName;
private int cPop = 0;
private int cArea = 0;
private int popDensity = 0;
private int popCounter = 0;
private int areaCounter = 0;
private int densityCounter = 0;
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()
{
for(int i = 0; i < theCountries.size(); i++)
{
if(cPop > popCounter)
{
popCounter = cPop;
finalPopName = cName;
}
}
return finalPopName;
}
public String largestArea()
{
for(int i = 0; i < theCountries.size(); i++)
{
if(cArea > areaCounter)
{
areaCounter = cArea;
finalAreaName = cName;
}
}
return finalAreaName;
}
public String popDensity()
{
for(int i = 0; i < theCountries.size(); i++)
{
if(popDensity > densityCounter)
{
densityCounter = popDensity;
finalDensityName = cName;
}
}
return finalDensityName;
}
}
这是CountriesTest类
import java.util.*;
public class CountriesTest
{
public static void main(String[] args)
{
int population = 0;
int area = 0;
int density = 0;
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();
System.out.print("Enter area of country in square kilometers: ");
String input2 = myScanner.nextLine();
population = Integer.parseInt(input2);
System.out.print("Enter population of country: ");
String input3 = myScanner.nextLine();
area = Integer.parseInt(input3);
density = population/area;
Countries aCountries = new Countries(input1, population, area, density);
}
else if(choice == 2)
{
System.out.println("The country with the largest population: " );
System.out.println("The country with the largest area: " );
System.out.println("The country with the largest population density is: " );
}
else if(choice == 3)
{
done = true;
}
else
System.out.println("Invalid Choice");
}
while (!done);
System.exit(0);
}
}
答案 0 :(得分:1)
好的,您的代码中存在一些混淆。
您正在使用&#34;国家&#34;同时为某个国家/地区和国家/地区列出课程。我不会推荐它,但至少你应该做出静态&#34;用于国家清单的成员和方法。或者您可以声明List theCountries = new ArrayList&lt;&gt;();而在主方法内部。
您永远不会添加新的&#34;国家&#34;反对国家名单。所以,如果您已经在主要方法中声明了国家,那么只需使用&#34; theCountries.add(aCountries)&#34;紧接着&#34;新国家(...)&#34;。
您的搜索方法(如largestPop)不会起作用,因为他们从不搜索&#34; theCountries&#34;的内容。数组列表。 (&#34; i&#34;变量只是遍历索引,但从未实际用于从此ArrayList获取一个位置)。
和btw,不需要System.exit(0)(暗示)