java打印不同的答案

时间:2017-07-15 19:04:19

标签: java string

期望影响 我想要我的代码首先,通过使用起始密码子“ATG”找到基因并且如果TAA或ATG正在重新调整空白字符串,则停止密码子“TAA”。然后代码应该检查Gene是否可以被3整除,因此确定一个真正的基因然后打印结果。我想使用void testFindSimpleGene测试代码。

现实 打印的所有基因都是空白字符串,每个空白字符串打印几次

public class findSimpleGeneAndTest {

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);

}

}

1 个答案:

答案 0 :(得分:1)

%(modulo)的优先级高于-(减号),因此您的表达式的计算结果为:

(stopIndex + 3) - (startIndex % 3)

我认为你的意思是:

((stopIndex + 3) - startIndex) % 3

可以缩短为

(stopIndex - startIndex) % 3