【问题标题】:Trouble correctly using boolean method along with using recursive methods麻烦正确使用布尔方法以及使用递归方法
【发布时间】:2019-07-18 03:24:47
【问题描述】:

我目前正在开发一个使用递归的项目。现在我有 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 方法出现问题,我要么卡住输出打印真或假,要么输出打印什么都没有,没有错误。 我试过这样的东西,但没有结果

   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

任何帮助将不胜感激。

【问题讨论】:

    标签: java arrays recursion methods boolean


    【解决方案1】:

    这里你只是打印isLarge的结果:

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

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

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

    【讨论】:

    • 嗨,谢谢你的帮助,很遗憾,我不知道为什么,但这并没有在输出中打印出任何东西。我也试过 'if (list[n-1].isLarge()) System.out.println(list[n-1].getName()); printLargeCountries(list,n-1);'并且国家/地区的输出是正确的,但带有 java.lang.ArrayIndexOutOfBoundsException: -1 error
    • @D.Bianc 你还需要if (n&gt;0) 声明和其他一切,你知道吗?你删除了吗?
    • 出于某种原因我不知道为什么,但我并没有想到要保留它们,哎呀。我把语句放回去了,代码运行良好,谢谢你的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2013-05-09
    相关资源
    最近更新 更多