我想知道是否可以为我想在代码中评估的字符串数组创建一个循环。我想一次做多个二进制数。到目前为止,我的工作正常,但我不知道如何让它一次评估多个二进制数。谢谢。
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 );
}
}
答案 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);
}
答案 1 :(得分: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);
}
答案 2 :(得分:1)
如果您至少运行Java 5,则可以使用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.
}
答案 3 :(得分: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
答案 4 :(得分: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