Java-如何将大整数解析为命令行参数?

Java-如何将大整数解析为命令行参数?,java,int,Java,Int,我需要编写一个适用于所有n值的短程序。n是命令行参数(args[0])。问题是Integer.parseInt不适用于较大的值,例如20000000。我能做些什么来解决这个问题?该程序设计用于打印2的幂的所有值,直到该值>=n且n必须为参数[0] public class PowerOfTwo{ public static void main(String[] args){ int k = 1; int n = Integer.parseInt(args[0]); if(

我需要编写一个适用于所有n值的短程序。n是命令行参数(args[0])。问题是Integer.parseInt不适用于较大的值,例如20000000。我能做些什么来解决这个问题?该程序设计用于打印2的幂的所有值,直到该值>=n且n必须为参数[0]

public class PowerOfTwo{
public static void main(String[] args){
    int k = 1;
    int n = Integer.parseInt(args[0]);
    if(n < 0){
        System.out.println("");
    }else{
        for(int i=1; k <= n; i++){
            k *= 2;
            if(k <= n){
                System.out.println(k);
            }else{
                System.out.println();
            }
        }
    }
}
公共类PowerOfTwo{
公共静态void main(字符串[]args){
int k=1;
int n=Integer.parseInt(args[0]);
if(n<0){
System.out.println(“”);
}否则{

对于(inti=1;k使用java.math.biginger或java.math.BigDecimal。它们可以处理任何大小的数字

然后,您的循环将如下所示:

public static void main(String[] args) {
    final BigInteger TWO = new BigInteger("2");
    BigInteger k = BigInteger.ONE;
    BigInteger n = new BigInteger(args[0]);
    if (n.compareTo(BigInteger.ZERO) < 0) {
        System.out.println("< 0");
    } else {
        while (k.compareTo(n) <= 0) {
            k = k.multiply(TWO);
            if (k.compareTo(n) <= 0) {
                System.out.println(k);
            } else {
                System.out.println();
            }
        }
    }
}
publicstaticvoidmain(字符串[]args){
最终的BigInteger 2=新的BigInteger(“2”);
BigInteger k=BigInteger.1;
BigInteger n=新的BigInteger(参数[0]);
if(n.compareTo(BigInteger.ZERO)<0){
System.out.println(“<0”);
}否则{

而(k.compareTo(n)您希望处理的最大值是什么

  • 对于最大为2^63-1的大数,可以使用long-有关的详细信息,需要将Integer.parseInt()更改为long.parseLong()
  • 如果你真的需要任意大的数字,那么使用。要做到这一点,你还需要将变量k改为BigInteger,使用而不是*=来构造变量n
尝试以下代码:

public class PowerOfTwo{
public static void main(String[] args){
    long k = 1;

    long n = Long.parseLong("20000000000");
    BigDecimal bigDec=new BigDecimal(n);
    if(n < 0){
        System.out.println("");
    }else{
        for(long i=1; k <= bigDec.longValue(); i++){
            k *= 2;
            if(k <= n){
                System.out.println(k);
            }else{
                System.out.println();
            }
        }
    }
  }
}

我认为最安全、最简单的方法是:

public class ReadBigInteger {
    public static void main(String[] args) {
        String number = args[0];
        if (number == null) {
            throw new IllegalArgumentException();       
        }

        System.out.println("The number: " + number);
        char [] tmp = number.toCharArray();

        int result = 0;
        int index = 1;

        for (int t = tmp.length - 1; t >= 0; t--) {
            int n = Integer.parseInt(tmp[t] + "");

            int toAdd = (int) (n == 0 ? (Math.pow(10, index-1)) : (n * (Math.pow(10, index-1))));

            if (toAdd > Integer.MAX_VALUE - result) {
                throw new IllegalArgumentException("Number to large");
            }
            result += toAdd;
            index++;
        }

        System.out.println("Entered integer: " + result);
    } 
}

基本上,将参数作为字符串读取。通过在过程中构建整数,以相反的顺序对其进行分析。如果结果大于
integer,则会引发异常。MAX\u VALUE

尝试将字符串传递到BigInteger(字符串)中构造函数?使用long只会将问题推到更大的数字。谢谢,这是因为我们的代码评估程序需要n的值:)非常感谢。这似乎比我在当前课程中学到的要复杂一点,但我认为我能理解。谢谢你的解释。我没有使用BigInteger的经验。作业要求它适用于所有n值。因此BigInteger是正确的方法。
public class ReadBigInteger {
    public static void main(String[] args) {
        String number = args[0];
        if (number == null) {
            throw new IllegalArgumentException();       
        }

        System.out.println("The number: " + number);
        char [] tmp = number.toCharArray();

        int result = 0;
        int index = 1;

        for (int t = tmp.length - 1; t >= 0; t--) {
            int n = Integer.parseInt(tmp[t] + "");

            int toAdd = (int) (n == 0 ? (Math.pow(10, index-1)) : (n * (Math.pow(10, index-1))));

            if (toAdd > Integer.MAX_VALUE - result) {
                throw new IllegalArgumentException("Number to large");
            }
            result += toAdd;
            index++;
        }

        System.out.println("Entered integer: " + result);
    } 
}