導航:首頁 > 編程語言 > rectanglejava

rectanglejava

發布時間:2025-03-08 17:51:26

1. java編程 求矩形周長和面積

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()。

閱讀全文

與rectanglejava相關的資料

熱點內容
安卓機看視頻怎麼沒有小窗口 瀏覽:456
minecraft伺服器怎麼布置 瀏覽:306
怎麼把安卓的東西轉到已激活蘋果 瀏覽:852
停止服務doss命令 瀏覽:877
u盤占內存但該文件夾為空 瀏覽:611
伺服器怎麼更換重生點 瀏覽:34
收費api調用平台源碼 瀏覽:646
安卓怎麼自檢病毒 瀏覽:560
布卡雲伺服器 瀏覽:770
程序員是怎麼做系統的 瀏覽:742
燕窩溯源碼最大加工廠 瀏覽:936
黑馬程序員第28集 瀏覽:485
lcd單片機驅動 瀏覽:647
通達信主力拉升出貨指標公式源碼 瀏覽:639
廉潔pdf 瀏覽:18
批量用修改多個文件夾及子目錄 瀏覽:321
王者每個伺服器爆滿怎麼辦 瀏覽:951
安卓手機如何清除所有的數據 瀏覽:983
激光打標機加密狗驅動無法啟動 瀏覽:912
矽谷程序員題庫 瀏覽:565