class Rectangle{
private int width;
private int height;
public Rectangle(int width,int height) {
this.setWidth(width);
this.setHeight(height);
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
if (width<=0) {
System.out.println("输入有误");
return;
}else{
this.width = width;
}
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
if (height<=0) {
System.out.println("输入有误");
return;
}
else{
this.height = height;
}
}
int getZhouchang(){
return (this.getWidth()+this.getHeight())*2;
}
int getArea(){
return this.getHeight() * this.getWidth();
}
}
public class Test1 {
public static void main(String[] args) {
Rectangle R=new Rectangle(1, 10);
System.out.println(R.getZhouchang());
System.out.println(R.getArea());
}
}
计算长方形的周长和面积
最新推荐文章于 2024-05-31 00:00:00 发布