2014-10-27 91 views
0

我需要編寫一個適用於所有n值的簡短程序。 n是一個命令行參數(args [0])。問題是Integer.parseInt不適用於較大的值,如20000000000.我能做些什麼來解決這個問題?該程序旨在打印所有值爲2的值,直到該值> = n且n必須爲參數[0]。Java - 如何解析大型int作爲命令行參數?

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(); 
      } 
     } 
    } 
} 

}

+2

請嘗試將字符串傳遞到BigInteger(String)構造函數中,否則將引發異常? – 2014-10-27 11:34:34

回答

2

使用java.math.BigInteger的或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(); 
      } 
     } 
    } 
} 
+0

非常感謝。在我目前的課程中,這似乎比我學到的要複雜一些,但我想我可以理解它。 – AugustM 2014-10-27 11:51:00

0

什麼是您所期待進程的最大價值?

  • 對於大量達到2^63 - 1,就可以使用長 - 見data types的更多信息,你需要的Integer.parseInt()更改爲的Long.parseLong()。
  • 如果您真的需要任意大號碼,請使用BigInteger。要做到這一點,你還需要改變你的變量k是一個BigInteger和使用BigInteger.multiply()而不是* =構建您的變量n使用new BigInteger(String value)
+0

感謝您的解釋。我沒有使用BigInteger的經驗。該分配要求它適用於n的所有值。所以BigInteger會是正確的選擇。 – AugustM 2014-10-28 07:46:42

0

嘗試下面的代碼:

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(); 
      } 
     } 
    } 
    } 
} 

輸出:

2 
4 
8 
16 
32 
64 
128 
256 
512 
1024 
2048 
4096 
8192 
16384 
32768 
65536 
131072 
262144 
524288 
1048576 
2097152 
4194304 
8388608 
16777216 
33554432 
67108864 
134217728 
268435456 
536870912 
1073741824 
2147483648 
4294967296 
8589934592 
17179869184 
+0

使用long只會將問題推到更大的數字。 – BarrySW19 2014-10-27 11:44:22

+0

謝謝,這是爲了我們的代碼評估程序需要的值n – AugustM 2014-10-27 11:52:34

0

我認爲這樣做最安全和最簡單的方法是:

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_VALUE