2012-03-13 51 views
0

我想知道是否可以爲我想在我的代碼中評估的字符串數組創建一個循環。我想一次執行多個二進制數。到目前爲止,我的工作正常,但是我無法確定如何使它一次評估多個二進制數。謝謝。字符串數組以及如何循環它們

package twoComplement; 

public class bintodec { 

public static void main (String[] args)throws java.io.IOException { 

int number, 
    digit1, 
    digit2, 
    digit3, 
    digit4, 
    digit5, 
    digit6, 
    digit7, 
    digit8, 
    result; 

String num = "11100111"; 

number = Integer.parseInt(num); 

digit1 = ((number % 100000000) - (number % 10000000 % 10000000))/10000000; 
digit2 = ((number % 10000000) - (number % 10000000 % 1000000))/1000000; 
digit3 = ((number % 1000000) - (number % 1000000 % 100000))/100000; 
digit4 = ((number % 100000) - (number % 100000 % 10000))/10000; 
digit5 = ((number % 10000) - (number % 10000 % 1000))/1000; 
digit6 = ((number % 1000) - (number % 1000 % 100))/100; 
digit7 = ((number % 100) - (number % 100 % 10))/10; 
digit8 = (number % 10); 

result = (digit1 * -128) + (digit2 * 64) + (digit3 * 32) + (digit4 * 16) + (digit5 * 8) + (digit6 * 4) + (digit7 * 2) + (digit8 * 1); 

System.out.println ("Binary number: " + num + "\nDecimal Number: " + result); 
System.out.println(); 
System.exit(0); 

} 
} 

回答

3

是的,這是一個推進了循環循環 ;-)的目的是通過數組進行迭代的最好方式。您還可以遍歷集合(IE:ArrayList),這可以更輕鬆地添加新項目。

String[] numbersToEvaluate = new String[]{"11100111", "100101", "10101101"}; 

for (String num: numbersToEvaluate) 
{ 
    number = Integer.parseInt(num); 

    digit1 = ((number % 100000000) - (number % 10000000 % 10000000))/10000000; 
    digit2 = ((number % 10000000) - (number % 10000000 % 1000000))/1000000; 
    digit3 = ((number % 1000000) - (number % 1000000 % 100000))/100000; 
    digit4 = ((number % 100000) - (number % 100000 % 10000))/10000; 
    digit5 = ((number % 10000) - (number % 10000 % 1000))/1000; 
    digit6 = ((number % 1000) - (number % 1000 % 100))/100; 
    digit7 = ((number % 100) - (number % 100 % 10))/10; 
    digit8 = (number % 10); 

    result = (digit1 * -128) + (digit2 * 64) + (digit3 * 32) + (digit4 * 16) + (digit5 * 8) + (digit6 * 4) + (digit7 * 2) + (digit8 * 1); 

    System.out.println ("Binary number: " + num + "\nDecimal Number: " + result); 
} 
+0

工作完美,非常感謝。如有可能,我會標記爲答案。 – 2012-03-13 16:47:26

1

這是定義字符串數組方式:

int n = 10; 
String[] arr = new String[n]; 

這裏是如何遍歷數組:

for (int i = 0; i < n; i++) { 
    arr[i] = "element number " + i; 
} 

或者這樣說:

for (String element : arr) { 
    System.out.println(element); 
} 
1

如果您至少運行Java 5,則可以使用a for-each構造以循環任何任意數組或集合。

String[] nums = //init 
for (String num:nums) { 
    // Do work. 
} 

但是,如果你是在Java 1.4或更早版本,或者你關心的數組的索引,你需要使用傳統的for循環

String[] nums = //init 
for (int i = 0; i<nums.length; i++) { 
    String num = nums[i]; 
    // Do work. 
} 
1

你的意思是這樣

for (String num : "11100111,1010,11111,110101010101010101011101010101010101010101".split(",")) { 
    long result = Long.parseLong(num, 2); 
    System.out.println("Binary number: " + num + ", Decimal Number: " + result); 
} 

打印

Binary number: 11100111, Decimal Number: 231 
Binary number: 1010, Decimal Number: 10 
Binary number: 11111, Decimal Number: 31 
Binary number: 110101010101010101011101010101010101010101, Decimal Number: 3665040856405 
+0

內置的Integer.parseInt()轉換幾乎是作弊:) – ggrigery 2012-03-13 17:26:57

+1

@ggrigery它不應該是一個問題,除非這是'[作業]';) – 2012-03-13 17:29:03

0

是的,這很容易。

String[] array = {"first", "second", "third"}; 
for (String element : array) { 
    // do whatever 
} 

但是,除此之外,您可能希望考慮以下更清晰的代碼。

String num = "1000"; 
int digit1 = Character.digit(num.charAt(0), 2); 
// if you are only expecting a binary number then the second argument (2) 
// tells the digit method to throw an error if it gets a digit other than 0 or 1 
... 

如果這是一個學校練習或類似的,你可能希望測試你的代碼對內置的Integer分析器。

int numFromBinaryString = Integer.parseInt(numberString, 2); 
// again, here the second argument tells parseInt to interpret numberString as 
// binary string 
相關問題