導航:首頁 > 編程語言 > 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相關的資料

熱點內容
單片機串口發射怎麼回事 瀏覽:472
程序員假裝自己很忙 瀏覽:798
程序員能力關鍵詞 瀏覽:615
plc編程高級視頻教程 瀏覽:610
java遞歸求n 瀏覽:84
python絕對路徑導入 瀏覽:126
nex5g加密 瀏覽:975
18的空島伺服器地址 瀏覽:86
程序員要學什麼硬體 瀏覽:666
股票漲跌源碼怎麼看 瀏覽:576
加密軟體做法 瀏覽:57
美國程序員有多少中國人 瀏覽:741
人民日報app里怎麼看新聞早班車 瀏覽:587
忘了app怎麼辦 瀏覽:531
如何用雲伺服器做雲平台 瀏覽:301
非箍筋加密區剪力 瀏覽:119
利聯科技伺服器卡怎麼辦 瀏覽:386
js和python哪個好 瀏覽:463
c盤的哪些文件夾沒用 瀏覽:83
文件為什麼會超出在線解壓限制 瀏覽:591