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

熱點內容
地推統計圖app怎麼做 瀏覽:162
android文件夾不能用了 瀏覽:857
加密軟體的日誌 瀏覽:892
腎病pdf 瀏覽:230
網站怎麼用本地伺服器 瀏覽:120
javadoc導入android 瀏覽:517
文件掃描成pdf 瀏覽:696
涼山火災救援命令 瀏覽:54
壓縮機三相檢測 瀏覽:861
linux怎麼安裝光碟 瀏覽:799
寬頻伺服器無響應是為什麼 瀏覽:226
壓縮包內看圖 瀏覽:914
安卓手機如何發現app後台調用情況 瀏覽:561
程序員簡歷工作描述 瀏覽:159
伺服器中強制疾跑的指令是什麼 瀏覽:54
日語命令性 瀏覽:261
免費的程序編譯游戲 瀏覽:892
如何安裝屏蔽的APP 瀏覽:198
豬臉識別app如何下載 瀏覽:156
卓嵐串口伺服器如何使用 瀏覽:440