我目前正在使用递归的项目。现在我有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);
  }
}


当尝试重新编写我的代码时,void方法可能会出现问题,我要么卡住了输出打印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


任何帮助将不胜感激。

最佳答案

在这里,您只是打印isLarge的结果:

System.out.println(list[n-1].isLarge());


您应该使用if语句检查isLarge的结果,然后打印国家/地区:

if (list[n-1].isLarge()) {
    System.out.println(list[n-1].getName());
}

关于java - 使用 boolean 方法以及使用递归方法时均出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54854094/

10-11 22:28
查看更多