有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Math abs和ceil输出编译错误

我需要帮助。我试着用数学课来计算一些数字。我可以编译代码,但从来没有得到输出。我正在使用Jcreator编译器。任何人都知道我的代码出了什么问题。谢谢你的意见 这是我的密码

public class Exercise607
{
    public static void main (String[] args)
    {
        double x=7.5;
        double xx;
        System.out.printf("the absolute value of x is %.2f\n", xx(x));
    }

    public static double xx( double x )
    {
        return Math.abs(x);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    您的本地double xx从未被使用过。此外,格式String只包含一个浮动说明符。我想你想要

    public static void main(String[] args) {
        double x = -7.5;
        System.out.printf("the absolute value of %.2f is %.2f%n", x, xx(x));
    }
    
    public static double xx(double x) {
        return Math.abs(x);
    }
    

    输出是

    the absolute value of -7.50 is 7.50