0

所以,我应该写一个程序来确定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
4

2 回答 2

1

代码中存在一些问题,如下所示:

  1. 运行第一个循环,直到除法余数为 0
  2. 计算除法过程中找到的数字
  3. 在第一个循环后将索引减 1,因为它在 while 循环中后加 1

示例更正的代码如下:

    int exp = holdDigits.length;
    System.out.println("exp: " + exp);
    int index = 0;
    while (n > 0){
        holdDigits[index] = n%10;
        System.out.println(index + "index: " + holdDigits[index]);
        n = n/10;
        System.out.println("remainder: " + n);
        index++;
    }
    index--;
    int count= index;
    while (index >=0 ){
        reverseNumber += holdDigits[count-index]*Math.pow(10,index);
        index--;
        System.out.println("sum so far: " + sum);
    }
于 2012-10-31T02:46:46.640 回答
0

如果您已经得到答案,我深表歉意,但这是使用带有 for 循环的数组获取反向数字的一种简短方法。

    var val = prompt("enter number");
    var New = val.split("");
    var arr1 = [];
    console.log(New);
    for (i = New.length - 1; i >= 0; i--) {
        arr1 += New[i]  + ',';
    } console.log(arr1);
于 2018-03-13T11:24:46.310 回答