[英]Conversion from parsing a String to double is returning 0.0:
這是我就該主題提出的最后一個問題的后續措施。 但是,這是一個不同的問題。
我的代碼正常工作,除了它使用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);
}
}
}
}
替換行convert = tempBinary.toString();
有:
convert = "";
for(int bin : tempBinary){
convert += bin;
}
那應該使您的轉換正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.