如何理解 java 递归中的 return 值
how to understand the return value in java's recursion
我用 java 语言编写了一个程序,但答案总是不正确,我使用递归来完成程序,但方法中的 return 值不是我想要一个,我调试的时候可以return两次it.if谁能帮我解释一下,非常感谢。
/**
* addDigits:
* Given a non-negative integer num * repeatedly add all
* its digits until the result has only one digit.
* For example:
* Given num = 38, the process is like:
* 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
* it should be 2,but the answer is 11,may anyone help me with the problem?
* thanks
*/
public class Test{
public int addDigits(int num) {
String str = String.valueOf(num);
String[] temp = new String[str.length()];
int tempInt = 0;
if (str.length() > 1) {
for (int i = 0; i < str.length(); i++) {
temp[i] = str.substring(i, i + 1);
tempInt += Integer.parseInt(temp[i]);
}
addDigits(tempInt);
} else {
tempInt = num;
}
return tempInt;
}
public static void main(String[] args) {
Test test = new Test();
int i = test.addDigits(38);
System.out.println(i);
}
}
当您在函数内部递归调用 addDigits(tempInt);
时,
你没有对结果做任何事情,你只是把它扔掉了。
将行更改为此将修复它:
tempInt = addDigits(tempInt);
此外,您可以更优雅地解决这个问题,而无需转换为字符串:
if (num < 10) {
return num;
}
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return addDigits(sum);
那是因为您忘记从对 addDigits()
的每次调用中捕获 return 值,请将您的代码替换为以下行:
tempInt = addDigits(tempInt);
我认为这里的问题是当您一遍又一遍地调用代码时,我尝试使用 do 循环代替:
public int addDigits(int num) {
int tempInt;
String str;
do
{
str = String.valueOf(num);
String[] temp = new String[str.length()];
tempInt = 0;
if (str.length() > 1) {
for (int i = 0; i < str.length(); i++) {
temp[i] = str.substring(i, i + 1);
tempInt += Integer.parseInt(temp[i]);
}
num = tempInt;
}
} while (str.length() > 1);
tempInt = num;
return tempInt;
}
public static void main(String[] args) {
Temp test = new Temp();
int i = test.addDigits(38);
System.out.println(i);
}
}
你的方法是将值返回到它一遍又一遍地调用它的地方,直到第 3 次或第 4 次才返回到 main。
我用 java 语言编写了一个程序,但答案总是不正确,我使用递归来完成程序,但方法中的 return 值不是我想要一个,我调试的时候可以return两次it.if谁能帮我解释一下,非常感谢。
/**
* addDigits:
* Given a non-negative integer num * repeatedly add all
* its digits until the result has only one digit.
* For example:
* Given num = 38, the process is like:
* 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
* it should be 2,but the answer is 11,may anyone help me with the problem?
* thanks
*/
public class Test{
public int addDigits(int num) {
String str = String.valueOf(num);
String[] temp = new String[str.length()];
int tempInt = 0;
if (str.length() > 1) {
for (int i = 0; i < str.length(); i++) {
temp[i] = str.substring(i, i + 1);
tempInt += Integer.parseInt(temp[i]);
}
addDigits(tempInt);
} else {
tempInt = num;
}
return tempInt;
}
public static void main(String[] args) {
Test test = new Test();
int i = test.addDigits(38);
System.out.println(i);
}
}
当您在函数内部递归调用 addDigits(tempInt);
时,
你没有对结果做任何事情,你只是把它扔掉了。
将行更改为此将修复它:
tempInt = addDigits(tempInt);
此外,您可以更优雅地解决这个问题,而无需转换为字符串:
if (num < 10) {
return num;
}
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return addDigits(sum);
那是因为您忘记从对 addDigits()
的每次调用中捕获 return 值,请将您的代码替换为以下行:
tempInt = addDigits(tempInt);
我认为这里的问题是当您一遍又一遍地调用代码时,我尝试使用 do 循环代替:
public int addDigits(int num) {
int tempInt;
String str;
do
{
str = String.valueOf(num);
String[] temp = new String[str.length()];
tempInt = 0;
if (str.length() > 1) {
for (int i = 0; i < str.length(); i++) {
temp[i] = str.substring(i, i + 1);
tempInt += Integer.parseInt(temp[i]);
}
num = tempInt;
}
} while (str.length() > 1);
tempInt = num;
return tempInt;
}
public static void main(String[] args) {
Temp test = new Temp();
int i = test.addDigits(38);
System.out.println(i);
}
}
你的方法是将值返回到它一遍又一遍地调用它的地方,直到第 3 次或第 4 次才返回到 main。