当我尝试编译时,Math.min(stop1, Math.min(stop2, stop3)) 出现“不兼容的类型:int 无法转换为 java.lang.String”错误。我不明白为什么会遇到这个问题,因为我不希望将整数更改为字符串来运行 min() 方法。任何想法?
import edu.duke.*;
import java.io.*;
import java.lang.*;
public class TagFinder {
public String findStopIndex(String dna, int index) {
int stop1 = dna.indexOf("tag", index);
if (stop1 == -1 || (stop1 - index) % 3 != 0) {
stop1 = dna.length();
}
int stop2 = dna.indexOf("tga", index);
if (stop2 == -1 || (stop2 - index) % 3 != 0) {
stop2 = dna.length();
}
int stop3 = dna.indexOf("taa", index);
if (stop3 == -1 || (stop3 - index) % 3 != 0){
stop3 = dna.length();
}
return Math.min(stop1, Math.min(stop2, stop3));
}
}
最佳答案
您正在尝试在此处返回 int
值:
return Math.min(stop1, Math.min(stop2, stop3));
但是 findStopIndex
的返回类型是 String
。
如果您确实需要 String
作为返回类型,只需将 int
转换为 String
即可:
return String.valueOf(Math.min(stop1, Math.min(stop2, stop3)));
如果不这样做,请更改方法签名以返回正确的类型:
public int findStopIndex(String dna, int index)
关于java - BlueJ 中与 Math.min 不兼容的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35602942/