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

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


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


例如 -


input - "5a-f5-11"

output- The numbers are 5, 5 and 11, therefore the sum is 21.

此任务中的问题是对多于一位数的数字求和。(在我们的例子中是 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);

}


凤凰求蛊
浏览 98回答 3
3回答

慕后森

好吧,这是使用递归解决问题的一种简单方法。我使用正则表达式来获取数字,因为您没有表示不允许这样做。   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;   }

婷婷同学_

这就是我的处理方式:public class AddMultipleDigitsInStringRecursive{&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; String input = "5a-f5-11";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Input: " + input);&nbsp; &nbsp; &nbsp; &nbsp; int sum = sumDigits(input);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Sum: " + sum);&nbsp; &nbsp; }&nbsp; &nbsp; public static int sumDigits(String input)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return addDigits(input, 0, "");&nbsp; &nbsp; }&nbsp; &nbsp; private static int addDigits(String input, int index, String curNumber)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(index < input.length())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int curSum = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String curChar = input.substring(index, index + 1); // get the current character (as a String)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Character.isDigit(curChar.charAt(0))) // if it's a digit, append it to the current number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curNumber = curNumber + curChar;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else // it's not a digit, do we have a number pending?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!curNumber.isEmpty())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curSum = Integer.parseInt(curNumber); // convert current number to an Integer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curNumber = ""; // reset the current number so we can accumulate more digits when they are found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // return the sum of the current number (if any) with any other numbers that are found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return curSum + addDigits(input, index + 1, curNumber);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else // reached the end of the string; was there a number pending?&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int curSum = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!curNumber.isEmpty())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curSum = Integer.parseInt(curNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return curSum;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; }}

潇湘沐

不知何故设法弄清楚了,它确实有效:public static int sum(String str, int i, String subStr, int sum) {&nbsp; &nbsp; if(str.length() <= i) return sum;&nbsp; &nbsp; String newSubStr = subStr;&nbsp; &nbsp; char oneStr = str.charAt(i);&nbsp; &nbsp; if(!Character.isDigit(oneStr)) //if the new index is not a number and the index before IS a number>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(isNumeric(subStr))// new index IS a number, therefore will sum up.&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int num = Integer.parseInt(subStr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum+=num;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newSubStr = "";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; String temp = oneStr+"";&nbsp; &nbsp; &nbsp; &nbsp; newSubStr += temp;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(sum);&nbsp; &nbsp; return sum(str, i+1, newSubStr, sum);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java