gpt4 book ai didi

循环中的Java boolean 条件

转载 作者:行者123 更新时间:2023-11-30 08:45:43 24 4
gpt4 key购买 nike

我正在尝试计算并返回两个长度相等的 DNA 序列不同的地方。例如,给定字符串“ATGT”和“GTGA”,结果应该是数组 { true, false, false, true }。我收到错误消息 False/true cannot be resolved to a variable heres what I got so far

/**
* 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com