Recusion-计算混合字符串中的数字总和。 (作业死胡同)

时间:2019-06-01 14:36:29

标签: java recursion

在尝试修复某些行时,我陷入了死胡同 在我的代码中。

任务是编写一个带字符串的递归函数并计算 其中的数字总和。

例如-

input - "5a-f5-11"
output- The numbers are 5, 5 and 11, therefore the sum is 21.

此任务中的Catch是对超过一位的数字求和。 (在我们的例子中为11)

我没有计划使用Char数组或其他任何东西,但是使用某些行中的字符串会很困难。

我的代码到目前为止还没有编译,但是我确定逻辑是正确的-(我做了一个辅助函数,这意味着它不是最终函数,而是主要工作)。

public static int  sumNumbersInText(String str, int i, String subStr, int sum) {

    if(str.length() >= i) return sum;

    char[] array = new char[str.length()];
    str.getChars(0, str.length(), array, 0);

    char oneStr = array[i];
    String newSubStr = subStr;
    if(Character.isDigit(oneStr)); //if the new index is not a number and the index before IS a number>
    {

        if(Character.isDigit(subStr));// new index IS a number, therefore will sum up.
        {
            int num = 0;
            sum+=num;
            newSubStr = "";
        }
    }

    else                             
    {
        newSubStr += oneStr;
    }

    return sumNumbersInText(str, i+1, subStr, sum);
}

3 个答案:

答案 0 :(得分:0)

这是我的处理方式:

public class AddMultipleDigitsInStringRecursive
{
    public static void main(String[] args)
    {
        String input = "5a-f5-11";
        System.out.println("Input: " + input);
        int sum = sumDigits(input);
        System.out.println("Sum: " + sum);
    }

    public static int sumDigits(String input)
    {        
        return addDigits(input, 0, "");
    }

    private static int addDigits(String input, int index, String curNumber)
    {
        if(index < input.length())
        {
            int curSum = 0;
            String curChar = input.substring(index, index + 1); // get the current character (as a String)
            if (Character.isDigit(curChar.charAt(0))) // if it's a digit, append it to the current number
            {
                curNumber = curNumber + curChar;
            }
            else // it's not a digit, do we have a number pending?
            {
                if (!curNumber.isEmpty())
                {
                    curSum = Integer.parseInt(curNumber); // convert current number to an Integer
                }
                curNumber = ""; // reset the current number so we can accumulate more digits when they are found
            }   
            // return the sum of the current number (if any) with any other numbers that are found
            return curSum + addDigits(input, index + 1, curNumber); 
        }
        else // reached the end of the string; was there a number pending?
        {
            int curSum = 0;
            if (!curNumber.isEmpty())
            {
                curSum = Integer.parseInt(curNumber);
            }
            return curSum;
        }  
    }
}

答案 1 :(得分:0)

以某种方式设法弄清楚了,它实际上有效:

public static int sum(String str, int i, String subStr, int sum) {
    if(str.length() <= i) return sum;
    String newSubStr = subStr;
    char oneStr = str.charAt(i);

    if(!Character.isDigit(oneStr)) //if the new index is not a number and the index before IS a number>
    {
        if(isNumeric(subStr))// new index IS a number, therefore will sum up.
        {
            int num = Integer.parseInt(subStr);
            sum+=num;
            newSubStr = "";
        }
    }
    else                             
    {
        String temp = oneStr+"";
        newSubStr += temp;
    }
    System.out.println(sum);
    return sum(str, i+1, newSubStr, sum);
}

答案 2 :(得分:0)

好吧,这是使用递归解决问题的一种简单方法。我使用了一个正则表达式来获取数字,因为您没有表明不允许这样做。

   public static int sum(String a) {
      Matcher m = Pattern.compile("(\\d\\d+)").matcher(a);
      if (m.find()) {
         return Integer.parseInt(m.group(1)) + sum(a.substring(m.end()));
      }
      return 0;
   }