Я прохожу курс Java на Coursera, но застрял здесь уже несколько дней.
Эти два метода принадлежат одному классу и очень похожи, за исключением типа возвращаемого значения и одного дополнительного. переменная в методе, которая не работает.
Это не работает (код компилируется, но не выполняется в BlueJ/не отображает результаты в консоли в Eclipse):
public int countAllGenes(String dna) {
int count = 0;
int startIndex = 0;
while (true) {
String gene = findGene(dna, startIndex);
if(gene.isEmpty()) {
break;
}
count = count + 1;
startIndex = dna.indexOf(gene, startIndex) + gene.length();
}
return count;
}
Но это работает:
public void printAllGenes(String dna) {
int startIndex = 0;
while (true) {
String gene = findGene(dna, startIndex);
if (gene.isEmpty()) {
break;
}
System.out.println(gene);
startIndex = dna.indexOf(gene, startIndex) + gene.length();
}
}
Добавление всего кода ниже:
public class Part1 {
public int findStopCodon(String dna, int startIndex, String stopCodon) {
// This method returns the index of the first occurrence of stopCodon
// that appears past startIndex and is a multiple of 3 away from startIndex.
// If there is no such stopCodon, this method returns the length of the dna strand.
int currIndex = dna.indexOf(stopCodon, startIndex+3);
while (currIndex != -1) {
int diff = currIndex - startIndex;
if (diff % 3 == 0) {
return currIndex;
}
else {
currIndex = dna.indexOf(stopCodon, currIndex);
}
}
return dna.length();
}
public String findGene (String dna, int startingPt) {
// Parse the strand and find if the sets of letters occur
int startIndex = dna.indexOf("ATG", startingPt);
int taaIndex = findStopCodon(dna, startIndex, "TAA");
int tagIndex = findStopCodon(dna, startIndex, "TAG");
int tgaIndex = findStopCodon(dna, startIndex, "TGA");
// Return the gene formed from the "ATG" and the closest stop codon
int currMin = Math.min(taaIndex, tagIndex);
int minIndex = Math.min(currMin, tgaIndex);
// If there is no valid stop codon and therefore no gene, return the empty string
if (minIndex == dna.length()) {
return "";
}
return dna.substring(startIndex, minIndex + 3);
}
public void printAllGenes(String dna) {
int startIndex = 0;
while (true) {
String gene = findGene(dna, startIndex);
if (gene.isEmpty()) {
break;
}
System.out.println(gene);
startIndex = dna.indexOf(gene, startIndex) + gene.length();
}
}
public int countAllGenes(String dna) {
int count = 0;
int startIndex = 0;
while (true) {
String gene = findGene(dna, startIndex);
System.out.println(startIndex);
if (gene.isEmpty()) {
break;
}
count = count + 1;
startIndex = dna.indexOf(gene, startIndex) + gene.length();
System.out.println(startIndex);
}
System.out.println(count);
return count;
}
public static void main (String[] args) {
Part1 p1 = new Part1();
p1.printAllGenes("ATGxxxTAAxxxATGxxxTAGxxxATGxxxTGA");
p1.countAllGenes("ATGxxxTAAxxxATGxxxTAGxxxATGxxxTGA");
}
}
При запуске консоль Eclipse просто отображает результаты p1.printAllGenes("ATGxxxTAAxxxATGxxxTAGxxxATGxxxTGA");, как показано ниже:
ATGxxxTAA
ATGxxxTAG
ATGxxxTGA
Подробнее здесь: https://stackoverflow.com/questions/784 ... her-doesnt