2012-10-30 67 views
-1

可能重複:
Decimal Conversion error十進制轉換爲八進制的轉換

我寫了一類的程序,並且有麻煩搞清楚如何將一個八進制數轉換爲十進制數。這是我一直在努力,到目前爲止做:

import java.util.Scanner; 

public class test 
{ 
public static void main (String args[]) 
{ 
    Scanner input = new Scanner(System.in); 

    System.out.print("Enter number: "); 
    int oct = input.nextInt(); 
    int d2count = 0; 
    int result=0; 
    int d3count = 0; 
    int d3 = 0; 
    int d2 = 0; 

    d3 = oct; 
    d2 = oct; 

    if(oct < 1000){ 
    while (d3 >= 100){ 
    d3 = d3 - 100; 
     d3count++; 
    }} 

    if (oct < 100){ 
    while (d2 >= 10){ 
    d2 = d2 - 10; 
     d2count++; 
    }} 

    result = (d3count * 64)+(d2count * 8) + d2; 
System.out.printf("%d\n",result); 
} 
} 

所以基本上我需要一種方法來減少一些到個位數(即1337到1,3,3,7)。 我真的想用我現在擁有的東西來做,但我的做法似乎有一些我看不見的錯誤。如果我輸入一個小於100的數字,它實際上是有效的,但是當我輸入一個高於100的數字時,轉換就會在某處出現。我是新來的Java所以更基本的己技巧越好,謝謝

+0

你真的想要結果作爲int? – hyde

+0

你剛剛幾個小時前也問過這個問題了... http://stackoverflow.com/questions/13142977/decimal-conversion-error/13143884#13143884請不要爲你已經發布的問題開始新線程 –

回答

3

從十進制到八進制的轉換之後,

進口java.util.Scanner的;

public class test { 
    public static void main (String args[]) { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter number: "); 
     int oct = input.nextInt(); 
     String result= Integer.toString(oct,8); 
     System.out.println(result); 
    } 
} 

從八進制下轉換爲十進制,

public static void main (String args[]) { 
    Scanner input = new Scanner(System.in); 
    System.out.print("Enter number: "); 
    String oct = input.next(); 
    int result= Integer.parseInt(oct,8); 
    System.out.println(result); 
} 

以下是從八進制轉換爲十進制數的多個算法的方式,

 public static int convert(String oct) { 
     int i= 0; 
     for(int j = 0; j < oct.length(); j++) { 
       char num = oct.charAt(j);   
       num -= '0';  
       if(num<0||num>7) {   
        sysout("invalid number"); 
        return -1; 
       } 
       i *= 8;       
       i += num;      
      } 
      return i; 
     } 
    } 

以下是用於將小數到八進制,

public static int convert(int OctalNumber){ 
    int counter=0; 
    int result = 0; 
    while(OctalNumber !=0) { 
     int temp = (int) ((OctalNumber%8) * Math.pow(10, counter)); 
     counter++; 
     result += temp; 
     OctalNumber /= 8; 
    } 
    return result; 
} 
+0

從他的代碼的外觀我認爲他實際上是將八進制轉換爲十進制。 –

+0

因此,他可能會錯誤地編碼,因爲他說他想將小數轉換爲八進制。 – Arham

+0

他更可能錯誤地輸入了他的頭銜,因爲他說他的代碼適用於其他輸入。此外,因爲這是一個任務,我假定他會尋找一個實際的算法。 –

0

首先,您的代碼將八進制轉換爲十進制。

其次,你有

if(oct < 1000){ 

if (oct < 100){ 

,但你有沒有if語句來處理案件中,輸入數量大於1000

編輯:我只是意識到這確實不是問題。當您開始計算d2時,您需要使用d3的結尾值,然而在多次減去100之後。前一秒所以,正確的,如果你需要一個

d2 = d3; 

你仍然需要處理輸入大於1000,雖然。

0

你可以用一點更好的算法,允許合理的長度(即任意十進制數的走了。不會導致溢出):

public int octalToDecimal(int octal) 
{   
    int numDigits = Integer.toString(octal).length(); 
    int decimalResult = 0; 
    int mask = 0x00000007; 
    for(int i = 0; i < numDigits; i++) 
    { 
     int octalDigit = octal & mask; 
     octal = octal >> 3;  
     decimalResult += octalDigit * Math.pow(8, i); 
    } 

    return decimalResult; 
} 
+0

我認爲你的'mask'應該是7,而不是0xF。此外,您應該使用10的冪,而不是8的冪。而且,您應該將「八進制」移位3,而不是1. – hyde

+0

更正第一個和第三個賬戶,但是爲什麼要使用10的冪乘以八進制數?這與將二進制數字乘以10的冪相同 - 沒有意義。 –

+0

是的,我猜,變量名'decimalResult'把我扔掉了。我的錯。 – hyde

0

這helper方法可能會有所幫助:

int strangeDecimalAsBaseN(int number, int base) { 
    if (base < 2 || base > 10) throw new InvalidArgumentException("Impossible or unsupported base " + base); 
    if (number < 0) throw new InvalidArgumentException("Negative number (" + number + ") not supported"); 
    int result = 0; 
    int shift = 1; 
    while(number > 0) { 
    int lastResult = result; 
    result += shift * (number % base); 
    if (lastResult > result) throw new IllegalStateException("Overflow!"); 
    shift *= 10; 
    number /= base; 
    } 
    return result; 
} 

或許這一點,如果你真的想用另一種方式:

int strangeFixBaseN(int funnyNumber, int base) { 
    if (base < 2 || base > 10) throw new InvalidArgumentException("Impossible base " + base); 
    if (funnyNumber < 0) throw new InvalidArgumentException("Negative number (" + funnyNumber + ") not supported"); 
    int result = 0; 
    int shift = 1; 
    while(funnyNumber > 0) { 
    int lastResult = result; 
    result += shift * (funnyNumber % 10); 
    if (lastResult > result) throw new IllegalStateException("Overflow!"); 
    shift *= base; 
    funnyNumber /= 10; 
    } 
    return result; 
} 

當心,未經測試的代碼:)