import java.util.Scanner;
class Rectangle {
private double length, width;
public Rectangle (double length,double width){
super();
this.length=length;
this.width=width;
}
public Rectangle(double side){
super();
this.length=side;
this.width=side;
}
public Rectangle(){
}
public double getLength(){
return length;}
public void setLength(double length){
if(length<0)
{
System.exit(-2);
}
this.length=length;
}
public double getWidth(){
return width;
}
public void setWidth(double width){
if(width<0){
System.exit(-2);
}
this.width=width;
}
public double getPerimeter(){
return 2*(width+length);
}
public double getArea(){
return width*length;
}
}
public class TestRectangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the length and width of rectangle r1:");
double length = sc.nextDouble();
double width = sc.nextDouble();
System.out.println("Please enter the side length of square r2:");
double side = sc.nextDouble();
Rectangle r1 = new Rectangle(length, width);
Rectangle r2 = new Rectangle(side);
Rectangle r3 = new Rectangle();
System.out.printf("The perimeter of r1 is %.2f\n", r1.getPerimeter() );
System.out.printf("The area of r2 is %.2f\n", r2.getArea() );
System.out.printf("The perimeter of r3 is %.2f\n", r3.getPerimeter() );
r3.setLength(r1.getLength());
r3.setWidth(r2.getWidth());
System.out.printf("After modification, the area of r3 is %.2f\n", r3.getArea() );
System.out.println("Please enter the length and width of rectangle r3:");
double r3_length = sc.nextDouble();
double r3_width = sc.nextDouble();
sc.close();
r3.setLength(r3_length);
r3.setWidth(r3_width);
System.out.printf("After modification, the area of r3 is %.2f\n", r3.getArea() );
}
}
为什么最后一行没有输出呢