2015-10-05 46 views
0

我創建一個程序,它在公制系統中佔用一個基本單位。 (說克。)然後當你選擇一個前綴改變它的等值。 (例如,當您選擇Kilo時,1000克會將其更改爲1公斤)。使用BigIntegers

問題是,當我運行代碼時,它總是最終爲零,這讓我認爲我在操縱BigIntegers錯誤。 (我使用非常大的數字,因爲一些前綴是非常小的或超出通常長一些非常大)

下面是代碼:

import java.util.Scanner; 
import java.math.BigInteger; 

public class BaseMetricUnits { 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    System.out.println("Hello, World!"); 
    float meter = 1; 
    float kilogram = 1; 
    float second = 1; 
    float ampere = 1; 
    float kelvin = 1; 
    float mole = 1; 
    float candela = 1; 
    PrefixConverter(); 
} 
public static void PrefixConverter() 
{ 

    BigInteger Yocto = BigInteger.valueOf((long) 0.00000000000000000000000); 
    BigInteger Zepto = BigInteger.valueOf((long) 0.000000000000000000001); 
    BigInteger Atto = BigInteger.valueOf((long) 0.000000000000000001); 
    BigInteger Femto = BigInteger.valueOf((long) 0.000000000000001); 
    BigInteger Pico = BigInteger.valueOf((long)0.000000000001); 
    BigInteger Nano = BigInteger.valueOf((long)0.000000001); 
    BigInteger Micro = BigInteger.valueOf((long)0.000001); 
    BigInteger Milli = BigInteger.valueOf((long)0.001); 
    BigInteger Centi = BigInteger.valueOf((long)0.01); 
    BigInteger Deci = BigInteger.valueOf((long)0.1); 
    BigInteger Deca = BigInteger.valueOf((long)10); 
    BigInteger Hecto = BigInteger.valueOf((long)100); 
    BigInteger Kilo = BigInteger.valueOf((long)1000); 
    BigInteger Mega = BigInteger.valueOf((long)1000000); 
    BigInteger Giga = BigInteger.valueOf((long)1000000000); 
    BigInteger Tera = new BigInteger("1000000000000"); 
    BigInteger Peta = new BigInteger("1000000000000000"); 
    BigInteger Exa = new BigInteger("1000000000000000000"); 
    BigInteger Zetta = new BigInteger("1000000000000000000000"); 
    BigInteger Yotta = new BigInteger("1000000000000000000000000"); 

    long Amount; 
    double Prefix; 
    String Units = ""; 
    BigInteger translatedResult; 
    BigInteger Result; 

    Scanner inputDevice = new Scanner(System.in); 
    System.out.print("Please enter the type of unit to be used. (meters, grams, etc.) >> "); 
    Units = inputDevice.next(); 
    System.out.print("Please enter an amount to be translated >> "); 
    Amount = inputDevice.nextLong(); 
    System.out.print("Please choose one of the following Prefixes to translate to. "); 
    System.out.print(" 1 - Yocto "); 
    System.out.print(" 2 - Zepto "); 
    System.out.print(" 3 - Atto "); 
    System.out.print(" 4 - Femto "); 
    System.out.print(" 5 - Pico "); 
    System.out.print(" 6 - Nano "); 
    System.out.print(" 7 - Micro "); 
    System.out.print(" 8 - Milli "); 
    System.out.print(" 9 - Centi "); 
    System.out.print(" 10 - Deci "); 
    System.out.print(" 11 - Deca "); 
    System.out.print(" 12 - Hecto "); 
    System.out.print(" 13 - Kilo "); 
    System.out.print(" 14 - Mega "); 
    System.out.print(" 15 - Giga "); 
    System.out.print(" 16 - Tera "); 
    System.out.print(" 17 - Peta "); 
    System.out.print(" 18 - Exa "); 
    System.out.print(" 19 - Zetta "); 
    System.out.print(" 20 - Yotta ") ; 
    Prefix = inputDevice.nextDouble(); 

    if(Prefix == 1) 
    { 
     Result = Yocto.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Yocto.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Yocto" + Units + "."); 
    } 
    if(Prefix == 2) 
    { 
     Result = Zepto.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Zepto.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Zepto" + Units + "."); 
    } 
    if(Prefix == 3) 
    { 
     Result = Atto.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Atto.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Atto" + Units + "."); 
    } 
    if(Prefix == 4) 
    { 
     Result = Femto.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Femto.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Femto" + Units + "."); 
    } 
    if(Prefix == 5) 
    { 
     Result = Pico.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Pico.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Pico" + Units + "."); 
    } 
    if(Prefix == 6) 
    { 
     Result = Nano.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Nano.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Nano" + Units + "."); 
    } 
    if(Prefix == 7) 
    { 
     Result = Micro.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Micro.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Micro" + Units + "."); 
    } 
    if(Prefix == 8) 
    { 
     Result = Milli.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Milli.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Milli" + Units + "."); 
    } 
    if(Prefix == 9) 
    { 
     Result = Centi.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Centi.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Centi" + Units + "."); 
    } 
    if(Prefix == 10) 
    { 
     Result = Deci.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Deci.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Deci" + Units + "."); 
    } 
    if(Prefix == 11) 
    { 
     Result = Deca.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Deca.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Deca" + Units + "."); 
    } 
    if(Prefix == 12) 
    { 
     Result = Hecto.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Hecto.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Hecto" + Units + "."); 
    } 
    if(Prefix == 13) 
    { 
     Result = Kilo.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Kilo.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Kilo" + Units + "."); 
    } 
    if(Prefix == 14) 
    { 
     Result = Mega.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Mega.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Mega" + Units + "."); 
    } 
    if(Prefix == 15) 
    { 
     Result = Giga.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Giga.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Giga" + Units + "."); 
    } 
    if(Prefix == 16) 
    { 
     Result = Tera.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Tera.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Tera" + Units + "."); 
    } 
    if(Prefix == 17) 
    { 
     Result = Peta.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Peta.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Peta" + Units + "."); 
    } 
    if(Prefix == 18) 
    { 
     Result = Exa.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Exa.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Exa" + Units + "."); 
    } 
    if(Prefix == 19) 
    { 
     Result = Zetta.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Zetta.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Zetta" + Units + "."); 
    } 
    if(Prefix == 20) 
    { 
     Result = Yotta.multiply(BigInteger.valueOf(Amount)); 
     translatedResult = Yotta.divide(BigInteger.valueOf(Amount)); 
     System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Yotta" + Units + "."); 
    } 
    else 
    { 
     System.out.println("Not a valid input."); 
    } 
    } 
} 

感謝您的幫助。

+2

'(長)0.000000000000000000001 == 0L'等演員基本上採用數量的整數部分。你有沒有很好的理由不使用像「Tera」,「Peta」等那樣的「0.000000000000000000001」?或'BigInteger.ONE.shiftRight/Left'? –

+0

我剛剛意識到我混合了乘法和除法的方法,說有些答案仍然出現爲0. – dpolaristar

+0

@AndyTurner像第一個BigInteger Yocto這樣的小數字,例如它會拋出我這些奇怪的錯誤。 異常在線程 「主」 java.lang.NumberFormatException:對於輸入字符串:」 0.00000" \t在java.lang.NumberFormatException.forInputString(未知來源) \t在java.lang.Integer.parseInt(未知源) \t位於java.math.BigInteger。 (Unknown Source) \t at java.math.BigInteger。 (來源不明) \t在BaseMetricUnits.PrefixConverter(BaseMetricUnits.java:21) \t在BaseMetricUnits.main(BaseMetricUnits.java:16) 像這些。 – dpolaristar

回答

1

BigInteger只能存儲整數,所以不適合這個應用程序的數據類型。我強烈建議用BigDecimal替換它。

你也應該用分數的字符串表示初始化:對這個答案和問題

BigDecimal Zepto = new BigDecimal("0.000000000000000000001"); 
    BigDecimal Atto = new BigDecimal("0.000000000000000001"); 
    BigDecimal Femto = new BigDecimal("0.000000000000001"); 
    BigDecimal Pico = new BigDecimal("0.000000000001"); 
    BigDecimal Nano = new BigDecimal("0.000000001"); 

評論表明使用的BigDecimal存儲較大的數字值得關注。該程序說明了一個事實,這是不是一個問題:

import java.math.BigDecimal; 

public class Test { 
    public static void main(String[] args) { 
    BigDecimal googol = new BigDecimal("1e100"); 
    System.out.println(googol); 
    System.out.println(googol.add(BigDecimal.ONE)); 
    } 
} 

輸出:

1E+100 
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 
+0

如果你向下看代碼,它會將結果吐出「結果」和「翻譯結果」。除非我能想出一種方法,使其成爲基於上下文的BigInteger或BigDecimal,否則最終答案可能是其中一個。我擔心如果用戶想要將一克轉換成Yottagram,然後BigInteger的答案會陷入一個錯誤,試圖存儲一個小數,反之亦然。 - – dpolaristar

+1

爲什麼你想要結果成爲BigInteger?如果答案不是整數,那將會丟失數據。另一方面,BigDecimal可以存儲任何BigInteger可以存儲的內容。 –

+0

因此,我可以存儲所有的數字,即使是BigDecimal的大數字?我不知道。 – dpolaristar