首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用BigIntegers

使用BigIntegers
EN

Stack Overflow用户
提问于 2015-10-06 06:37:09
回答 1查看 364关注 0票数 0

我创建了一个程序,它采用公制中的一个基本单位。(比方说克。)然后,当您选择一个前缀时,将其更改为相等的数量。(例如,选择Kilo时,1000克会将其更改为1千克。)

问题是,当我运行代码时,它总是以零结束,这让我认为我操作BigIntegers是错误的。(我使用非常大的数字,因为一些前缀非常小或非常大,超出了通常的长数字。)

代码如下:

代码语言:javascript
复制
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.");
     }
    }
}

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2015-10-06 06:55:09

BigInteger只能存储整数,因此不适合此应用程序的数据类型。我强烈建议用BigDecimal替换它。

您还应该使用分数的字符串表示来初始化:

代码语言:javascript
复制
  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存储较大数字的担忧。这个程序说明了这不是一个问题:

代码语言:javascript
复制
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));
  }
}

输出:

代码语言:javascript
复制
1E+100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32958997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档