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

标签 java compiler-errors min

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

相关文章:

Java 2D PNG : Calculate Dimension

C++ 枚举和内联 header 函数

mysql - 选择共享 MIN(PRICE) 值的多个书名 | MySQL

c++ - 在 C++ 中找到最小数 +ve 数?

java - slf4j/logback/jcl-over-slf4j - 即使 jcl-over-slf4j 不在依赖项中,仍然路由到 logback?

java - 全局序列比对动态规划寻找矩阵中的最小值

java - 使用 RTC 源代码管理获取已更改文件的文件名、位置和作者

python - python twitter api 1.1获取转推ID

.net - 阻止将特定的程序集添加到我的工厂项目中?

matlab - 来自具有最大/最小值的 accumarray 的索引