You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Rectangle类代码运行仅输出面积 无法输出周长问题求助

你的周长计算方法逻辑完全正确,无法输出周长的原因是main方法中仅调用了area方法打印结果,没有主动调用perimeter方法输出周长。

修正后的完整代码如下:

public class Rectangle
{
    public static void main(String[] args)
    {
        System.out.println("矩形面积:" + area(10,15));
        System.out.println("矩形周长:" + perimeter(10,15));
    }
/**
* Returns the area of a rectangle
*/
    public static int area (int length, int width)
    {
        return length * width;
    }
/**
* Returns the perimeter of the rectangle
*/
    public static int perimeter (int length, int width)
    {
        return 2 * (length + width);
    }
}

改动说明:

  • 仅在main方法中新增了调用perimeter方法打印的代码,原有两个计算方法的逻辑没有问题不需要修改
  • 额外增加了输出内容的前缀标识,方便区分两个输出结果,你可以根据需求调整输出格式

内容的提问来源于stack exchange,提问作者emily

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 17:15:04