查看單個文章
舊 2010-03-10, 09:42 PM   #1
9319074
註冊會員
 
9319074 的頭像
榮譽勳章

勳章總數0
UID - 331648
在線等級: 級別:3 | 在線時長:25小時 | 升級還需:7小時級別:3 | 在線時長:25小時 | 升級還需:7小時級別:3 | 在線時長:25小時 | 升級還需:7小時
註冊日期: 2009-10-05
文章: 90
精華: 0
現金: 32 金幣
資產: 156 金幣
Question 自己做的JAVA輸出敘述不知要如何改



--------------------
閱讀本主題的最佳解答
--------------------


package javaapplication7;

class Rectangle {
double length ; //長
double width ; //寬
double area;//面積
double perimeter;//周長
void printState(){

System.out.print("長為");
System.out.print(length+ " 公分,");
System.out.print("寬為");
System.out.print(width + "公分,");
System.out.print("面積為");
System.out.print(area + "cm^2");
System.out.print("周長為");
System.out.println(perimeter + "cm");

}


}
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();

rect1.length = 7;
rect1.width = 5;
rect1.area = (rect1.length*rect1.width );

rect2.length = 3;
rect2.width = 7;
rect2.perimeter = ((rect2.length*2)+(rect2.width*2));

System.out.print("矩行1");
rect1.printState();

System.out.print("矩行2");
rect2.printState();


}

}

他的輸出為:

run:
矩行1長為7.0 公分,寬為5.0公分,面積為35.0cm^2周長為0.0cm
矩行2長為3.0 公分,寬為7.0公分,面積為0.0cm^2周長為20.0cm
成功建置 (總時間:0 秒)

==分隔線==

假如我要輸出

矩行1長為7.0 公分,寬為5.0公分,面積為35.0cm^2
矩行2長為3.0 公分,寬為7.0公分,周長為20.0cm

請問要怎麼改,幫忙一下,謝謝。
9319074 目前離線  
送花文章: 35, 收花文章: 44 篇, 收花: 48 次
回覆時引用此帖