第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Java 矩形類面積/周長輸出0

Java 矩形類面積/周長輸出0

揚(yáng)帆大魚 2023-11-10 15:21:01
當(dāng)我運(yùn)行 Rectangle.java 時(shí),我可以獲得矩形中輸入的長度和寬度,但是當(dāng)我嘗試使用 getter 計(jì)算面積/周長時(shí),結(jié)果為零我嘗試添加和刪除 setter,在 getArea 和 getPerimeter 中放入 getter/setter 方法,但似乎沒有任何效果//Code provided by the teacher as a template Rectangle temp = new Rectangle();        temp.print();        System.out.println();        temp.setLength(2.5);        temp.setWidth(3.0);        //Consider how your rectangle will change after setting the length and width to specific values.        temp.print();        System.out.println();        Rectangle r = new Rectangle(3.5,2);        r.print();//My Classpublic class Rectangle {    private static double length;    private static double width;    private static double perimeter;    private static double area;    public Rectangle(double length, double width)    {        setLength(length);        setWidth(width);    }    public Rectangle()    {    }    public static double getLength()     {        return length;    }    public static void setLength(double length)     {        Rectangle.length = length;    }    public static double getWidth()     {        return width;    }    public static void setWidth(double width)    {        Rectangle.width = width;    }       public static double getPerimeter(double length, double width)     {        return 2*width+2*length;    }    public static double getArea(double length, double width)     {        area= getLength()*getWidth();        return length*width;    }    public static String print()     {        String Rectangle = new String();        System.out.println("This rectangle has a length of "+length+" and a width of "+width);        System.out.println("The area of the rectangle is: "+ area);        System.out.println("The perimeter of the rectangle is: "+ perimeter);        return Rectangle;    }}沒有錯(cuò)誤消息。輸出:該矩形的長度為 0.0,寬度為 0.0 矩形的面積為: 0.0 矩形的周長為: 0.0該矩形的長度為 2.5,寬度為 3.0 矩形的面積為: 0.0 矩形的周長為: 0.0該矩形的長度為 3.5,寬度為 2.0 矩形的面積為: 0.0 矩形的周長為: 0.0
查看完整描述

1 回答

?
慕蓋茨4494581

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊

您需要更改打印方法并調(diào)用周長和面積方法,以便這些變量獲得所需的值


 public static String print() 

{

    getPerimeter(length,width);

    getArea(length,width);

    String Rectangle = new String();

    System.out.println("This rectangle has a length of "+length+" and a width of "+width);

    System.out.println("The area of the rectangle is: "+ area);

    System.out.println("The perimeter of the rectangle is: "+ perimeter);

    return Rectangle;


}

我建議不要使用靜態(tài)關(guān)鍵字,因?yàn)橹挡粫?huì)綁定到對象


查看完整回答
反對 回復(fù) 2023-11-10
  • 1 回答
  • 0 關(guān)注
  • 135 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)