import java.util.*;
public class Rectangle {
private float length; //定義長變數
private float width; // 寬變數
public Rectangle(float length,float width){
this.length=length;
this.width=width;
}
public float getGirth(){
return (length+width)*2;
} //求周長方法
public float getArea(){
return length*width;
} //求面積方法
public static void main (String[] args) {
Scanner in=new Scanner(System.in);//調用輸入方法
System.out.println ("請輸入矩形的長:");
float a=in.nextFloat();
System.out.println ("請輸入矩形的寬:");
float b=in.nextFloat();
System.out.println ("矩形周長為:"+new Rectangle(a,b).getGirth());
System.out.println ("矩形面積為:"+new Rectangle(a,b).getArea());
}
}
//Jcreator4.0編譯通過,寫的比較簡單 只有簡單的功能 剛剛寫的求周長時忘乘2了...
2. Java編寫一個矩形類,並計算面積和周長
class Rectangle{
private int width = 2;
private int length = 1;
public int getWidth(){
return this.width;
}
public void setWidth(int w){
this.width = w;
}
public int getLength(){
return this.length;
}
public void setLength(int l){
this.length = l;
}
public int getArea(){
return this.length * this.width;
}
public int getCircumference(){
return (this.length + this.width) * 2;
}
public Rectangle(){}
public Rectangle(int l, int w){
this.length = l;
this.width = w;
}
}
public class demo{
public static void main(String[] args) {
Rectangle rect = new Rectangle(30, 8);
System.out.print("長方形的面積是:");
System.out.println(rect.getArea());
System.out.printf("長方形的周長是:%d\n", rect.getCircumference());
}
}
3. java怎麼算長寬
根據要求,步驟設計分別如下:
1、創建項目【Ex2_1】:
(3)rectanglejava擴展閱讀:
1、System.out.println解析。
System是一個類,繼承自根類Object。out是類PrintStream類實例化的一個對象,且是System類的靜態成員變數,println()是類PrintStream的成員方法,被對象out調用。
2、Scanner input=new Scanner(System.in)。
Scanner是一個類,是一個開源代碼,用他創建一個對象(input),System.in可以看做一個參數,這個參數是鍵盤輸入內容。
對象(input)有許多的方法如:input.next();指的是鍵盤輸入的文字內容,在用String a來接收就是:String a=input.next()。