2012-10-31 53 views
0

因此,我應該編寫一個程序,確定Ermips。我有其餘的想法,但我不知道如何正確地反向數字。我應該使用一個數組來扭轉它。使用數組的Java反向編號

例如,數量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 

完全搞明白了現在,由於約根德拉·辛格! 檢查出來:

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

笏是問題? – Metalhead

回答

1

中有如下代碼中的一些問題:

  1. 運行第一循環,直到除法的餘數爲0
  2. 計數在分裂過程
  3. 發現數字
  4. 第一環路之後由1減少指數爲在while循環它交增加由一個

示例更正後的代碼可能如下:

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

非常感謝!我現在完全明白了! – user1368970