gpt4 book ai didi

Java程序故障

转载 作者:行者123 更新时间:2023-11-30 03:07:13 25 4
gpt4 key购买 nike

我的问题的前半部分:当我尝试运行我的程序时,它会永远加载;它从不显示结果。有人可以检查我的代码并在某处发现错误吗?这个程序的目的是找到一个起始DNA密码子ATG,并一直寻找,直到找到终止密码子TAA或TAG或TGA,然后从头到尾打印出该基因。我正在使用 BlueJ。

我的问题的后半部分:我应该编写一个程序,其中需要执行以下步骤:

To find the first gene, find the start codon ATG.
Next look immediately past ATG for the first occurrence of each of the three stop codons TAG, TGA, and TAA.
If the length of the substring between ATG and any of these three stop codons is a multiple of three, then a candidate for a gene is the start codon through the end of the stop codon.
If there is more than one valid candidate, the smallest such string is the gene. The gene includes the start and stop codon.
If no start codon was found, then you are done.
If a start codon was found, but no gene was found, then start searching for another gene via the next occurrence of a start codon starting immediately after the start codon that didn't yield a gene.
If a gene was found, then start searching for the next gene immediately after this found gene.

请注意,根据此算法,对于字符串“ATGCTGACCTGATAG”,ATGCTGACCTGATAG 可能是一个基因,但 ATGCTGACCTGA 不会是一个基因,即使它更短,因为首先找到的另一个“TGA”实例不是倍数远离起始密码子的三个。

在我的作业中,我还被要求生成这些方法:

具体来说,要实现该算法,您应该执行以下操作。

Write the method findStopIndex that has two parameters dna and index, where dna is a String of DNA and index is a position in the string. This method finds the first occurrence of each stop codon to the right of index. From those stop codons that are a multiple of three from index, it returns the smallest index position. It should return -1 if no stop codon was found and there is no such position. This method was discussed in one of the videos.
Write the void method printAll that has one parameter dna, a String of DNA. This method should print all the genes it finds in DNA. This method should repeatedly look for a gene, and if it finds one, print it and then look for another gene. This method should call findStopIndex. This method was also discussed in one of the videos.
Write the void method testFinder that will use the two small DNA example strings shown below. For each string, it should print the string, and then print the genes found in the string. Here is sample output that includes the two DNA strings:

示例输出为:

ATGAAAATGAAAA

发现的基因是:

ATGAAATGA

DNA 字符串是:

ccatgccctaataaatgtctgtaatgtaga

发现的基因是:

atgccctaa

atgtctgtaatg标签

DNA 字符串是:

CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCA

发现的基因是:

ATGTAA

ATGAATGACTGATAG

ATGCTATGA

ATGTGA

我仔细考虑了一下,发现这段代码接近工作顺序。我只需要我的输出来产生说明中要求的结果。希望这不是太困惑,我只是不知道如何在起始密码子之后寻找终止密码子,然后如何获取基因序列。我还希望了解如何通过查找三个标签(tag、tga、taa)中哪一个更接近 atg 来获得最接近的基因序列。我知道这很多,但希望这一切都有意义。

import edu.duke.*;
import java.io.*;

public class FindMultiGenes {
public String findGenes(String dnaOri) {
String gene = new String();
String dna = dnaOri.toLowerCase();
int start = -1;
while(true){
start = dna.indexOf("atg", start);
if (start == -1) {
break;
}
int stop = findStopCodon(dna, start);
if(stop > start){
String currGene = dnaOri.substring(start, stop+3);

System.out.println("From: " + start + " to " + stop + "Gene: "
+currGene);}
}
return gene;
}

private int findStopCodon(String dna, int start){
for(int i = start + 3; i<dna.length()-3; i += 3){
String currFrameString = dna.substring(i, i+3);

if(currFrameString.equals("TAG")){
return i;

} else if( currFrameString.equals("TGA")){
return i;

} else if( currFrameString.equals("TAA")){
return i;

}
}
return -1;
}

public void testing(){


FindMultiGenes FMG = new FindMultiGenes();

String dna =
"CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCA";

FMG.findGenes(dna);




System.out.println("DNA string is: " + dna);

}
}

最佳答案

将行 start = dna.indexOf("atg", start); 更改为

start = dna.indexOf("atg", start + 1);

当前发生的情况是,您在索引 k 处找到 "atg",并在下一次运行中搜索字符串中的下一个 "atg"k 开始。由于包含起始位置,因此会在完全相同的位置找到下一个匹配项。因此,您将一遍又一遍地查找相同的索引k,并且永远不会停止。

通过将索引加 1,您可以跳过当前找到的索引 k 并开始从 k+1 开始搜索下一个匹配项。

关于Java程序故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34459060/

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