所以,我应该写一个程序来确定Ermips。我已经弄清楚了其余的部分,但我不确定如何正确反转数字。我应该使用数组来反转它。
例如,数字 357。
我使用 mod 运算符获取最后一位数字并将其放入数组的第一个索引中。
357%10 = 7
myArray[0] = 7
357/10 = 35
剩下的
使用剩余的 35 重新开始。
35%10 = 3
myArray[1] = 3
35/10 = 3 for a remainder
ETC。 ...
我需要基本上循环这个,所以我可以做任何长度的数字来反转它。
然后,在我拥有该数组之后,显示该数组以反向生成数字....753。
public class Reverse {
public static void main(String[]args) {
int n = 357;
int MAX_NUMBERS = 20;
int currentNumber = 0;
int reverseNumber = 0;
int remain = 0;
int sum = 0;
int [] holdDigits = new int [MAX_NUMBERS];
int exp = holdDigits.length;
System.out.println("exp: " + exp);
int index = 0;
//sum array
int count = holdDigits.length;
while (count > 0){
holdDigits[index] = n%10;
System.out.println(index + "index: " + holdDigits[index]);
n = n/10;
System.out.println("remainder: " + n);
count--;
index++;
}
while (index < holdDigits.length){
reverseNumber += holdDigits[index]*Math.pow(10,count-exp);
index--;
System.out.println("sum so far: " + sum);
}
System.out.println("Number reversed: " + reverseNumber);
}//end of main
}//end of class
现在完全明白了,感谢Yogendra Singh!看看这个:
public class Reverse2 {
public static void main(String[]args) {
int n = 76495;
int MAX_NUMBERS = 20;
int reverseNumber = 0;
int index = 0;
//declare an array to hold the digits while reversing
int [] holdDigits = new int [MAX_NUMBERS];
//the exponent is the number of spaced used in the array
int exp = holdDigits.length;
//while the number is greater than 0, use mod to put the right-most
//digit in index 0, divide the remaining number and increase the index
//to put it in the next open slot of the array.
while (n > 0){
holdDigits[index] = n%10;
n = n/10;
index++;
}
//decrease the index by one so it doesn't add the remaining zero as
//a placeholder in the number
index--;
//count is the index because below, you subtract it, making the display
//of the array reversed.
int count= index;
//while the index is greater than zero, by starting at the last filled
//slot of the array, the reverse number is added onto each time by
//multiplying the number times 10 to the power of whichever place it
//is which happens to be the index.
//EXAMPLE: to turn 7 into 700, multiply by 7x10^3
while (index >= 0 ){
reverseNumber += holdDigits[count-index]*Math.pow(10,index);
//lower the index to do the next number of the array
index--;
}
System.out.println("Reversed number: " + reverseNumber);
}//end of main
}//end of class