【发布时间】:2015-03-30 03:23:00
【问题描述】:
这是对我就该主题提出的最后一个问题的跟进。但这是一个不同的问题。
我的代码正在运行,除了它使用 copyOfRange 复制某种地址。由于某种地址,它总是返回 0.0,而不是数组 getBits 的部分。
有人可以扫描一下这个并提出建议吗?我为此快疯了(这不是作业)。
package runTests;
import java.util.Arrays;
public class runTestGetBinaryStrands {
protected static int getBits[] = {1,0,1,1,0,1,0,0,0,1,1,0,1,0,1,0};
double numerator, denominator, x, y;
public static void main (String[] args){
runTestGetBinaryStrands test = new runTestGetBinaryStrands();
test.getNumber(null, getBits);
}
/*NOTE OF THIS FORLOOP: * Divided the bits array in half & convert two different binary values to a string * I parsed the string to an int value, which can be put saved to a double and be treated like a decimal value. * I got the first 8 elements and stashed them into numerator, and did the same for denominator for the remaining array bits. * * The chromosome has one binary string, made up of a bunch of smaller parts.* You use getNumber in the chromosome to get out the values of the parts. **/
public void getNumber(String convert, int[] tempBinary){
for (int i = 0; i < getBits.length; i++){
for(int j = 0; j < getBits.length; j++){ //start at index 0 to 7 = 8.
tempBinary = Arrays.copyOfRange(getBits, 0, 7); //Get first set of 8 elements.
convert = tempBinary.toString();
System.out.println(convert);
try{
numerator = Integer.parseInt(convert); //converts string to one whole section in
}catch (NumberFormatException ex){
}
System.out.println("See Numerator's value: " + numerator);
tempBinary= Arrays.copyOfRange(getBits, 8, 15); //Get Second set of 8 elements.
convert = tempBinary.toString();
try{
denominator = Integer.parseInt(convert); //converts string to one whole section in
}
catch (NumberFormatException ex){
}
System.out.println("See Denominator's value: " + denominator);
}
}
}
}
【问题讨论】:
-
上一主题的参考:这不是重复的! stackoverflow.com/questions/29334506/…
-
Arrays.copyOfRange()中的第三个参数是独占的。因此,您想将 7 更改为 8 并将 15 更改为 16。另外,您希望convert = tempBinary.toString()做什么?你想要一个 1 和 0 的字符串,比如“10110100”吗?因为那不是正在发生的事情。 -
Quicksilver002,这正是我想要的。我以为数组是从 0 开始的?
-
我希望
convert = tempBinary.toString()(我的字符串:convert)将被分配 tempBinary 的值。从那里开始,我希望将其中的二进制值撕成一个数字。
标签: java arrays parsing type-conversion double