使用Array在Java中反转数字

时间:2012-10-31 02:26:36

标签: java arrays numbers reverse modulus

所以,我应该编写一个确定 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

等。 ...

我需要基本上循环这个,所以我可以做任何长度数来反转它。

然后,在我拥有该数组之后,显示数组以反向生成数字.... 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

2 个答案:

答案 0 :(得分:1)

代码中存在以下问题:

  1. 运行第一个循环,直到除法的余数为0
  2. 计算在除法流程中找到的数字
  3. 在第一次循环后将索引减少1,因为它在while循环中增加了一个
  4. 示例更正后的代码如下:

        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);
        }
    

答案 1 :(得分:0)

我道歉,如果你已经得到了答案,但这是使用带for 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);