我想編寫一個程序來確定使用遞歸字符串中的數字的總和,我認爲下面的代碼將打印「總和爲6」到控制檯,而是它輸出「代碼是150」。這段代碼的錯誤是什麼?
有人能告訴我我的錯誤是什麼嗎?
public class SumOfString {
public static String Test = new String();
public static Integer count = new Integer(0);
public static Integer sum = new Integer(0);
public static long sumThis(String s){
if (count< s.length()){
if (Character.isDigit(s.charAt(count))){
int digit = s.charAt(count);
count++;
sum += digit;
return sumThis(s);}
else{
count++;
return sumThis(s);}}
else{
return sum;}}
public static void main(String[] args) {
Test= "1a2b3c";
System.out.println("The sum is " + sumThis(Test));
}
是什麼'tstInt' – Mritunjay
一個錯誤,應該算是 – Wrolly13