[英]Can anyone explain this code for me?
public int sumOfDigits(int num){
return num == 0 ? 0 : num % 10 + sumOfDigits(num/10);
}
这是数字总和的递归计算-只要您没有消耗掉所有数字,它将最后一个与前面所有数字的总和相加。
sumOfDigits is a method which accepts int argument and the return type is also int. sumOfDigits是一种接受int参数并且返回类型也是int的方法。 If 0 is passed then it will return 0 other wise it will return num % 10 + sumOfDigits(num/10)
如果传递了0,则它将返回0,否则将返回num % 10 + sumOfDigits(num/10)
If a number other than 0 is passed this will continue as long as entered number becomes 0 如果传递了非0的数字,则只要输入的数字变为0,该数字就会继续
return num == 0 ? 0 : num % 10 + sumOfDigits(num/10);
Is equivalent to: 等效于:
if(num==0)
{
return 0;
}
else
{
return num % 10 + sumOfDigits(num/10); // this is recursive call. It gets called until num is reduce to 0
}
This basically calulates as following: 基本上计算如下:
If num=768
result= 7+6+8
. 如果num=768
result = 7+6+8
。
That is the Java ternary operator. 那是Java三元运算符。 It is similar to ternary operators in other languages. 它类似于其他语言中的三元运算符。 In this case: 在这种情况下:
return <boolean expression> ? <if true> : <if false>;
You have posted a recursive algorithm to calculate the sum of digits in an integer. 您已经发布了一个递归算法来计算整数中的数字之和。
Two equivalent methods are posted below: 下面发布了两种等效的方法:
public int sumOfDigitsIf(int num) {
if (num == 0) {
return 0;
} else {
return num % 10 + sumOfDigitsIf(num / 10);
}
}
public int sumOfDigitsLoop(int num) { // useful to examine if you aren't used to recursive algorithms
int value = 0;
while (num > 0) {
value += num % 10;
num /= 10;
}
return value;
}
This function calculates the sum of the digits of the number recursively. 此函数递归计算数字的总和。 First, if the number is 0, it returns 0, as the sum of the digits of 0 is 0. Then, it calculates the digit in the 1s place with num % 10, and adds that to the sum of the digits of the remaining places, num / 10 is the remaining places, and it calls sumOfDigits() on that number. 首先,如果数字为0,则返回0,因为0的数字之和为0。然后,它计算num%为10的1s位置的数字,并将其加到其余数字的总和中个位,num / 10是剩余的位,它在该数字上调用sumOfDigits()。
This method calculates sum of the numbers representing base 10 integer numbers. 此方法计算代表以10为基的整数的数字之和。 The method is called recursively with % operator to separate digits in the number and then add them to form the result. 用%运算符递归调用该方法以分离数字中的数字,然后将它们相加以形成结果。
Eg - 234 into 2 + 3 + 4 = 9 例如-234成2 + 3 + 4 = 9
However, if you pass a integer in base other than base 10, the method still gives the result for base 10 但是,如果在以10为底的底数中传递整数,则该方法仍会以10为底数给出结果
Eg sumOfDigits(0b1011010)
-> answer is 9 for 90 in base 10 sumOfDigits(0x532)
-> answer is 7 for 1330 in base 10 例如sumOfDigits(0b1011010)
->答案是9以10为基数sumOfDigits(0x532)
->答案是7以sumOfDigits(0x532)
为基数
Following is the change for binary and octal values to get the sum of the representing numbers 以下是对二进制和八进制值的更改,以获取代表数字的总和
return num == 0 ? 0 : num % 2 + sumOfDigits(num / 2);
return num == 0 ? 0 : num % 8 + sumOfDigits(num / 8);
That's a Java ternary operator, but in most cases it is not used since if-else statements are much more readable, which can also do the same function ternary operators can do. 那是Java三元运算符,但是在大多数情况下,由于if-else语句更具可读性,因此不使用它,它也可以执行三元运算符可以执行的相同功能。 But if you would like to make shorter code lines, then this is preferred. 但是,如果您想使代码行更短,那么这是首选。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.