为什么blueJ会尝试永远执行我的程序

时间:2019-05-15 21:32:55

标签: java bluej

我不确定我的代码是否有问题,但是当我尝试使用长度超过50个字符的字符串运行代码时,BlueJ尝试永远运行该程序,并且控制台永远不会出现。我将其运行了一个小时左右,但仍然没有任何结果。

我尝试直接在程序中输入各种字符串,输入的短链DNA似乎可以正常工作,但是经过一定长度后,程序永远不会执行,我不确定是什么原因,因为没有错误显示消息或异常。

package com.company;

public class problem3 {

    public static void main(String[] args) {
        new problem3().printAllGenes("CAATGCTGATAGTAATGGTATTATGATATGTAGTGGGATTTAGAGGATGCGCGCAGCCGATGACGAGCGACGATGCTAA");
    }

    public int findStopCodon(String dnaStr, int startIndex, String stopCodon) {
        int currIndex = dnaStr.indexOf(stopCodon, startIndex + 3);
        while (currIndex != -1) {
            int diff = currIndex - startIndex;
            if (diff % 3 == 0) {
                return currIndex;
            } else {
                currIndex = dnaStr.indexOf(stopCodon, currIndex+1);
            }
        }
        return dnaStr.length();
    }

    public String findGene(String dna, int where) {
        int startIndex = dna.indexOf("ATG", where);

        if (startIndex == -1) {
            return "";

        }
        int taaIndex = findStopCodon(dna, startIndex, "TAA");
        int tagIndex = findStopCodon(dna, startIndex, "TAG");
        int tgaIndex = findStopCodon(dna, startIndex, "TGA");
        int minIndex = 0;

        if (taaIndex == -1 || (tgaIndex != -1 && tgaIndex < taaIndex)) {
            minIndex = tgaIndex;

        } else {
            minIndex = taaIndex;
        }
        if (minIndex == -1 || (tagIndex != -1 && tagIndex < minIndex)) {
            minIndex = tagIndex;
        }
        if (minIndex == -1) {

            return "";
        }
        if (minIndex + 3 > dna.length()) {
            return "";
        }

        return dna.substring(startIndex, minIndex + 3);
    }

    public void printAllGenes(String dna) {

        int startIndex = 0;

        while (true) {

            System.out.println("yes");
            String currentGene = findGene(dna, startIndex);

            if (currentGene.isEmpty()) {
                break;
            }

            System.out.println(currentGene);

            startIndex = dna.indexOf(currentGene, startIndex) + currentGene.length();
        }

    }
}

该程序应该通过寻找起始密码子(ATG)和终止密码子(TAA,TAG,TGA)来获取DNA链和p基因。要运行该程序,我将使用String dna =“ CAATGCTGATAGTAATGGTATTATGATATGTAGTGGGATTTAGAGGATGCGCGCAGCCGATGACGAGCGACGATGCTAA”来调用printAllGenes()方法。

1 个答案:

答案 0 :(得分:0)

您可能需要更改

currIndex = dnaStr.indexOf(stopCodon, currIndex);

findStopCodon

currIndex = dnaStr.indexOf(stopCodon, currIndex + 1);

因为否则,您将始终找到相同的currIndex,并且永远不会退出while循环。