[英]Incompatible types with Math.min in BlueJ
当我尝试编译时,在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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.