我想知道是否可以爲我想在我的代碼中評估的字符串數組創建一個循環。我想一次執行多個二進制數。到目前爲止,我的工作正常,但是我無法確定如何使它一次評估多個二進制數。謝謝。字符串數組以及如何循環它們
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);
}
}
工作完美,非常感謝。如有可能,我會標記爲答案。 – 2012-03-13 16:47:26