2017-07-15 90 views
-2

會預期 我想要通過使用起始密碼子「ATG」我的代碼,首先,找到一個基因和終止密碼子「TAA」如果TAA orATG被mising到retun一個空字符串。然後,代碼應該檢查基因是否可以被3整除,從而得到真正的基因然後打印結果。我想用void testFindSimpleGene測試代碼。的Java打印不同的答案

現實 印刷的所有基因都是空白串和各空字符串被打印everal倍

公共類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); 

} 

}

+0

歡迎堆棧溢出!它看起來像你需要學習使用調試器。請幫助你一些補充性的調試技術。如果您之後仍然遇到問題,請隨時返回一個顯示您的問題的[最小化,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

回答

1

%(模)的優先級高於-(減號),因此您的表達被評估爲:

(stopIndex + 3) - (startIndex % 3) 

我想你的意思做:

((stopIndex + 3) - startIndex) % 3 

這可以縮短到

(stopIndex - startIndex) % 3 
+0

好,精確,簡潔。完善。 – GhostCat