I want to write a program that reverses a number (without converting it to a string). I have my code working for all numbers except numbers with leading zeros, for example: 500
and numbers like that. It simply gives me 5
when I want 005
.
Here is my code
System.out.println("Enter a number with at most 10 digits:");
long inputNumber = myKeyboard.nextLong();
long tempInputNumber = inputNumber;
long reversedNumber = 0;
int digits = 0;
int rightMostDigit;
while(tempInputNumber != 0) {
rightMostDigit = (int)tempInputNumber % 10;
reversedNumber = (reversedNumber*10)+rightMostDigit;
tempInputNumber = tempInputNumber / 10;
digits++;
}
如果您将每个数字作为一个数字存储在数组中,就像它们用于单热编码一样怎么办?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.