
正文
java求矩形面积的代码 java求矩形面积的方法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
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("长方形java求矩形面积的代码的面积是java求矩形面积的代码:");
System.out.println(rect.getArea());
System.out.printf("长方形的周长是java求矩形面积的代码:%d\n", rect.getCircumference());
}
}
相关问答
Q1: JAVA求助:由键盘输入一个矩形的长和宽,求出这个矩形的周长和面积 该怎么写
public static void main(String[] args) {
System.out.println("输入长度");
Double c=new Scanner(System.in).nextDouble();
System.out.println("输入宽度");
Double k=new Scanner(System.in).nextDouble();
System.out.println("面积:"+c*k);
System.out.println("周长:"+(c+k)*2);
}
Q2: 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());
}
}
关于java求矩形面积的代码和java求矩形面积的方法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






