【问题标题】:Reverse Number in Java using Array使用数组反转Java中的数字
【发布时间】:2012-10-20 11:11:39
【问题描述】:

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

【问题讨论】:

    标签: java arrays numbers reverse modulus


    【解决方案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);
        }
    

    【讨论】:

    • 非常感谢!我现在完全明白了!
    【解决方案2】:

    如果您已经得到答案,我深表歉意,但这是使用带有 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 2017-11-20
      相关资源
      最近更新 更多