我目前正在使用递归的项目。现在我有3个类,第一个有对象和变量,以及我的方法。
public class Country
{
private String name;
private String capital;
private double population;
public Country (String name, String capital, double population)
{
this.name = name;
this.capital = capital;
this.population = population;
}
public String getName()
{
return name;
}
public String getCapital()
{
return capital;
}
public boolean isLarge()
{
if (population > 50.00)
return true;
else
{
return false;
}
}
public double getPopulation()
{
return population;
}
public String toString()
{
return "\nName of Country: " + name + "\t Country's Capital: " + capital + "\t Population of Country: "
+ population + " Million"+"\n";
}
}
我的第二堂课拥有所有递归方法
public class CountryCollection
{
public static String toString(Country[] list, int n)
{
if (n==0)
return "";
else
return toString(list, n-1) + list[n-1].toString();
}
public static int countLargeCountries(Country[] list, int n)
{
if (n==0)
return 0;
else if (list[n-1].isLarge())
return countLargeCountries(list, n-1) + 1;
else
return countLargeCountries(list, n-1);
}
/*public static Country smallestCountry(Country[] list, int n)
{
if (n==1)
return list[0];
else if (list[n-1]<list[0])
list[0] = list[n-1];
return smallestCountry(list, n-1);
}*/
// keep geting bad operand type WILL FIX LATER dont know why
public static void printLargeCountries(Country[] list, int n)
{
if (n>0)
{
printLargeCountries(list, n-1);
System.out.println(list[n-1].isLarge());
}
}
}
这是我的代码有问题的地方。方法public static void printLargeCountries(Country[] list, int n)
无论国家/地区是否有其输出,输出都会输出是或否 人口是否超过5000万。像这样
true
true
false
false
false
false
false
true
false
false
false
false
我似乎无法弄清楚如何打印那些国家 人口,而不是我的输出为所有打印真或假 人口是否超过5000万的国家。
我希望将输出打印出来
(使用递归)
USA
France
Japan
或者只是
Name of Country: USA Country's Capital: Washington,D.C Population of Country: 326.17 Million
Name of Country: France Country's Capital: Paris Population of Country: 65.23 Million
Name of Country: Japan Country's Capital: Tokio Population of Country: 126.53 Million
然后,我的测试类从名为inData.txt
的输入文件进行扫描
public class TestCountryCollection
{
public static void main(String[]args) throws IOException
{
Country[] myList = new Country [12];
int myCount = 0;
Scanner fileScan;
double population;
String name;
String capital;
Country oneCountry;
fileScan = new Scanner (new File("inData.txt"));
while (fileScan.hasNext())
{
name = fileScan.next();
capital = fileScan.next();
population = fileScan.nextDouble();
oneCountry = new Country(name,capital,population);
myList[myCount] = oneCountry;
myCount++;
}
CountryCollection obj = new CountryCollection();
System.out.println("Country Info \n" + obj.toString(myList,myCount));
System.out.println("Number of Large Countries: " + obj.countLargeCountries(myList,myCount));
obj.printLargeCountries(myList,myCount);
}
}
当尝试用无效方法重写我的代码时,我要么卡住了输出true或false,要么输出就没有任何错误。 香港专业教育学院尝试过这样的事情,但没有结果
public static void printLargeCountries(Country[] list, int n)
{
if (n==0)
System.out.println("There are no Large Countries");
else if (list[n-1].isLarge())
System.out.println(list[n-1]);
}
这里是文本文档,以防万一。
USA Washington, D.C 326.17
France Paris 65.23
Slovenia Ljubljana 2.08
Slovakia Bratislava 5.44
Liechtenstein Vaduz 0.05
Monaco MonteCarlo 0.04
Spain Madrid 46.42
Japan Tokio 126.53
Afghanistan Kabul 35.53
Australia Canberra 24.61
Ghana Accra 28.83
Norway Oslo 5.26
任何帮助将不胜感激。
答案 0 :(得分:0)
这里您只是打印isLarge
的结果:
System.out.println(list[n-1].isLarge());
您应该使用if语句检查isLarge
的结果,然后打印国家/地区:
if (list[n-1].isLarge()) {
System.out.println(list[n-1].getName());
}