gpt4 book ai didi

java - BlueJ 中与 Math.min 不兼容的类型

转载 作者:行者123 更新时间:2023-12-02 03:54:42 28 4
gpt4 key购买 nike

当我尝试编译时,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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com