java打印不同的答案
期望值 我希望我的代码首先,通过使用起始密码子“ATG”和终止密码子“TAA”找到一个基因,如果TAA或TAG错误地返回一个空白字符串。然后,代码应该检查基因是否可以被3整除,从而得到一个真实的基因,然后打印结果。我想使用void testFindSimpleGene测试代码 现实 所有打印的基因都是空白字符串,每个空白字符串都会打印多次 公共类findSimpleGeneAndTest{java打印不同的答案,java,string,Java,String,期望值 我希望我的代码首先,通过使用起始密码子“ATG”和终止密码子“TAA”找到一个基因,如果TAA或TAG错误地返回一个空白字符串。然后,代码应该检查基因是否可以被3整除,从而得到一个真实的基因,然后打印结果。我想使用void testFindSimpleGene测试代码 现实 所有打印的基因都是空白字符串,每个空白字符串都会打印多次 公共类findSimpleGeneAndTest{ public String findSimpleGene(String dna) { String
public String findSimpleGene(String dna) {
String result = "";
int startIndex = dna.indexOf("ATG");//start codon is ATG
if( startIndex == -1){ //If there is no ATG return empty srting
return "";
}
int stopIndex = dna.indexOf("TAA", startIndex+3); //stop codon is TAA
if( stopIndex ==-1){ //If there is no TAA return empty srting
return "";
}
if ((stopIndex+3)-startIndex %3 != 0){ //Test that gene divisable by 3; a true gene
return "";
}
result = dna.substring(startIndex, stopIndex+3);
return result;
}
public void testFindSimpleGene(){
String dna = "AATGCGTAATATGGT";
System.out.println("DNA strand is " + dna);
String gene = findSimpleGene(dna);
System.out.println("Gene is "+gene);
dna = "AATGCTAGGGTAATATGGT";
System.out.println("DNA strand is " + dna);
gene = findSimpleGene(dna);
System.out.println("Gene is "+ gene);
dna = "ATCCTATGCTTCGGCTGCTCTAATATGGT";
System.out.println("DNA strand is " + dna);
gene = findSimpleGene(dna);
System.out.println("Gene is " + gene);
dna = "ATGTAA";
System.out.println("DNA strand is " + dna);
gene = findSimpleGene(dna);
System.out.println("Gene is " + gene);
}
}%
(模)的优先级高于-
(负),因此表达式的计算公式为:
(stopIndex + 3) - (startIndex % 3)
我想你的意思是:
((stopIndex + 3) - startIndex) % 3
可以缩短为
(stopIndex - startIndex) % 3
欢迎来到堆栈溢出!看起来您需要学习使用调试器。请随意使用一些补充调试技术。如果你以后仍然有问题,请随时回来用一个能证明你的问题的例子。好的,精确的,简洁的。完美的