【问题标题】:How to write a recursive method in java that takes in a positive or negative integer and returns the number of digits it has如何在java中编写一个递归方法,它接受一个正整数或负整数并返回它的位数
【发布时间】:2018-09-15 23:34:46
【问题描述】:

我正在尝试解决这个练习:编写一个递归方法,返回作为 int 类型参数传递给它的整数中的位数。允许积极和消极的论点。例如,-120 为三位数。

这是我拥有的代码,但是当我尝试传入 121 时,我一直只得到 1:

public static int recursion(int inNumber){
    //create a counter variable for the total of digits
    int totalDigits = 0;
    //base case
    if (inNumber < -10 || inNumber > 10){
        totalDigits++;
        return totalDigits;
        //recursive case
    }else{
        totalDigits++;
        return recursion(inNumber/10) + totalDigits;
    }
}

【问题讨论】:

  • 看起来你已经在 if 语句中切换了 。

标签: java recursion methods


【解决方案1】:

如果你拿121,

int totalDigits = 0;
if (121< -10 || 121> 10){ // YES 121>10 then: 
    totalDigits++;
    return totalDigits;

您的逻辑检查它是否高于 10,它是并返回 totalDigits (1)。你想做的是相反的。如果高于 10,则调用相同的函数。基本上你的 if/else 只是相反的。

【讨论】:

    【解决方案2】:

    再看看你的 if 条件。 121大于10,所以条件inNumber &gt; 10为真,执行totaldigits++,方法返回1。

    条件应改为if (inNumber &gt; -10 &amp;&amp; inNumber &lt; 10)

    【讨论】:

      【解决方案3】:

      试试这个简化的代码,使用Math.abs

      public static void main(String[] args) {
           System.out.println(recursion(123456, 0));
      }   
      
      public static int recursion(int inNumber, int totalDigits){
      
          totalDigits++;
          if (Math.abs(inNumber) < 10){
              return totalDigits;
          }else{
              return recursion(inNumber/10, totalDigits);
          }
       }
      

      输出

      6

      【讨论】:

      • 不需要第二个参数。
      • @leoderprofi - 是的,只是我个人的喜好。你的回答很好
      • 谢谢!这很酷,而且我以前从未见过 abs 方法,所以我学到了一些新东西:)
      【解决方案4】:

      你的方法的每次调用,你重置 totalDigits 的值,你应该将 totalDigits 作为递归调用的一部分影响到你的函数,你的代码应该是

      public static int recursion(int inNumber) {
          //create a counter variable for the total of digits
          //base case
          if (Math.abs(inNumber)<10) {
      
              return 1;
              //recursive case
          } else {
              return recursion(inNumber / 10) + 1;
          }
      
      }
      

      【讨论】:

      • 你用的是什么号码?
      • 嗯?所有数字?哦,我明白你的意思了,除了负面的我不好!
      • 老实说,如果你发布的答案有明显错误,你可以期待有人会叫你出来。这不是欺凌——这是试图保持 Stack Overflow 上的答案质量很高,这对每个人都有好处。在发布之前测试答案是一件的事情。我彻底推荐它。
      【解决方案5】:

      以下是我修复此代码的方法:

      1: 通过使用逻辑否定来解决逻辑错误并使您的代码更具描述性。

      public static int recursion(int inNumber) {
          //create a counter variable for the total of digits
          int totalDigits = 0;
          //base case
          if (!(inNumber <= -10 || inNumber >= 10)) { //notice the !
              return totalDigits + 1;
          //recursive case
          } else {
              totalDigits++;
              return recursion(inNumber / 10) + totalDigits;
          }
      }
      

      优化1:你也可以去掉局部变量totalDigits,因为它总是1

      public static int recursion(int inNumber) {
          //base case
          if (!(inNumber <= -10 || inNumber >= 10)) {
              return 1;
          //recursive case
          } else {
              return recursion(inNumber / 10) + 1;
          }
      }
      

      优化2:你可以使用Math.abs(int)实现同样的效果:

      public static int recursion(int inNumber){
          if (Math.abs(inNumber) < 10) {           //base case
              return 1;
          } else {                                 //recursive case
             return recursion(inNumber / 10) + 1;
          }
       }
      

      【讨论】:

      • 你可以去掉局部变量 totalDigits - 好点
      • 感谢您逐步完成此过程,这对您有很大帮助。我对此很陌生,这并不明显哈哈。
      • @trueboolean 只是一件事:您只能选择一个接受的答案,而不是全部(我注意到您更改了接受的答案)。不过,您可以投票赞成所有有用的答案。见meta.stackexchange.com/questions/5234/…
      • @leoderprofi 再次感谢,我试图检查所有帮助我的人,但我没有意识到这未检查以前检查的答案。我真的很感激你的崩溃。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2014-03-26
      • 1970-01-01
      • 2013-09-16
      • 2017-02-03
      相关资源
      最近更新 更多