我正在尝试计算并返回两个长度相等的 DNA 序列不同的地方。例如,给定字符串“ATGT”和“GTGA”,结果应为数组 { true, false, false, true }。我收到错误/true 无法解析为变量的错误,这是我到目前为止得到的
/**
* Calculates and returns where two DNA sequences of equal lengths differ. For
* example, given strings "ATGT" and "GTGA", the result should be array
* { true, false, false, true }.
*
* @param dna1 a String representing a DNA sequence of arbitrary length (equal
* to dna2's length), containing only the characters A, C, G and T
* @param dna2 a String representing a DNA sequence of arbitrary length (equal
* to dna1's length), containing only the characters A, C, G and T
* @return an array of boolean values, of length equivalent to both
* parameters' lengths, containing true in each subscript where the
* parameter strings differ, and false where they do not differ
*/
public static boolean[] mutationPoints(String dna1, String dna2) {
boolean [] mutPoint = new boolean [dna1.length()];
for( int i = 0; i < i; i++) {
if( dna1 != dna2) {
mutPoint[i] = False;
}
else if (dna1 == dna2) {
mutPoint[i] = True;
}
}
最佳答案
您需要迭代 mutPoint.length
次(而不是在循环中迭代 i
次)。您想要比较 i
索引处的字符(而不是 String
)。并且您需要返回数组。比如,
boolean[] mutPoint = new boolean[dna1.length()];
for (int i = 0; i < mutPoint.length; i++) {
mutPoint[i] = dna1.charAt(i) != dna2.charAt(i);
}
return mutPoint;
或喜欢
char[] dna1arr = dna1.toCharArray();
char[] dna2arr = dna2.toCharArray();
boolean[] mutPoint = new boolean[dna1arr.length];
for (int i = 0; i < mutPoint.length; i++) {
mutPoint[i] = dna1arr[i] != dna2arr[i];
}
return mutPoint;
关于Java 循环中的 boolean 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33161517/