在Java中,可以使用Math类的pow方法来计算一个数的平方。下面是计算两个数x和y的平方和的示例代码:

public class SquareSum {
    public static void main(String[] args) {
        double x = 2.5;
        double y = 3.8;
        
        double squareSum = Math.pow(x, 2) + Math.pow(y, 2);
        
        System.out.println('The square sum of ' + x + ' and ' + y + ' is ' + squareSum);
    }
}

在上面的代码中,我们使用Math.pow方法来计算x的平方和y的平方,并将结果存储在变量squareSum中。最后,我们使用System.out.println方法将结果打印到控制台。输出结果为:

The square sum of 2.5 and 3.8 is 19.29

原文地址: https://www.cveoy.top/t/topic/pkbO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录