运行Math.Round的错误

时间:2018-12-19 04:33:56

标签: java

我正在参加一些我的工作认可的Lynda.com课程,并希望学习一些Java。有人可以告诉我为什么我的Round和Absolute函数出错吗?

我看到的错误是“无法解析方法round(double)”。我得到了绝对值相同的错误。我所要做的几乎就是复制教师给我的东西。

我只是在做一些简单的数学计算:

public class Math {

    public static void main(String[] args) {
        int intValue1 = 56;
        int intValue2 = 42;

        int result1 = intValue1 + intValue2;
        System.out.println("Addition: " + result1);

        int result2 = intValue1 - intValue2;
        System.out.println("Subtraction: " + result2);

        int result3 = intValue1 * intValue2;
        System.out.println("Multiplication: " + result3);

        double result4 = (double) intValue1 / intValue2;
        System.out.println("Division: " + result4);

        int result5 = intValue1 % intValue2;
        System.out.println("Remainder: " + result5);

        double doubleValue = -3.999999;
        long rounded = Math.round(doubleValue);
        System.out.println("Rounded: " + rounded);

        double absValue = Math.abs(doubleValue);
        System.out.println("Absolute: " + absValue);
    }
}

也请让我知道我是否没有正确执行代码标签?

4 个答案:

答案 0 :(得分:2)

问题在于您的类被称为$(function(){ $('.naigation > li.x').mouseenter(function(){ $('#overlay').show(); }) .mouseout(function(){ $('#overlay').hide(); }) }) ,因此编译器正在您的类上寻找方法Math,该方法不存在。

将您的类重命名为round()或类似的名称,然后编译器将知道您需要MyJavaLesson中的方法。

java.lang.Math包下的任何内容中,您都不应使用与类相同的名称来命名自己的类,因为它通常会引起混乱(如此处所示)。

答案 1 :(得分:0)

查看将double doubleValue替换为float doubleValue是否对您有帮助。Math.round()函数以float为依据。 不要忘记在浮点数分配后添加f,即float doubleValue = -3.999f

答案 2 :(得分:0)

这里的主要问题是该类的名称为 Math (最顶部:公共类Math {)。它将覆盖Java提供的 Math 工具箱。如果您将您的班级命名为其他文件,那应该没问题。 ps。通常,按照惯例,我们在类和文件名中不使用大写字母;避免这些事情发生。

答案 3 :(得分:0)

感谢大家在此方面的帮助。另外,不确定是否允许在这个论坛上提出这个问题,但是只是好奇其他人如何学习Java?我正在考虑在社区大学上课,但想知道我是否可以从Lynda.com,Udemy和Coursera学习更好。