Я прохожу курс 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
Два метода Java очень похожи, но один работает, а другой нет. ⇐ JAVA
Программисты JAVA общаются здесь
-
Anonymous
1715958967
Anonymous
Я прохожу курс 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
Подробнее здесь: [url]https://stackoverflow.com/questions/78495422/two-java-methods-are-very-similar-but-one-runs-while-the-other-doesnt[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия