// Copy number to a temp variable so we have it later for the sum
int temp = number;
int reflectedNumber = 0;
while (temp != 0) {
int reflectedDigit = temp % 10;
temp /= 10;
reflectedNumber = reflectedNumber * 10 + refletedDigit;
}
// Now you have both the number and the reflected number - sum them:
int result = number + reflectedNumberl
public class Main {
public static void main(String[] args) {
int number = 123;
int reflectedNumber = 0;
int original = number;
while (number != 0) {
int lastDigit = number % 10;
reflectedNumber = reflectedNumber * 10 + lastDigit;
number = number / 10;
}
System.out.println("The reflected number is: " + reflectedNumber);
System.out.println("Sum of original and reflected = " + (original + reflectedNumber));
}
}
输出:
The reflected number is: 321
Sum of original and reflected = 444
3条答案
按热度按时间fkaflof61#
你就快到了。现在,你要打印出反映的数字,而不是把它们放在一起生成完整的数字。一旦你有了它,你就可以把这两个加起来:
vbkedwbf2#
您需要保留原始号码的备份,在计算完反射号码后,可以将其添加到反射号码中。此外,您不必只打印反映的数字;相反,您必须按如下所示进行计算:
输出:
p1tboqfb3#
只需将原始数字复制到另一个变量中,并将其添加到反映的数字中