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() );
}
}
为什么最后一行没有输出呢
你输入的r3的长宽是多少啊?
不知道你这个问题是否已经解决, 如果还没有解决的话:controller 层校验后,可以通过返回字符串的形式,提示前端异常信息;service 校验后,需要返回 Exception 给上层。这会导致底层校验产生大量的自定义 Exception,但底层校验相比于顶层校验能辐射系统的更多部分,要想保持系统的健壮性,底层校验是必须的。那么由于底层校验导致上层代码涌现很多 try catch 怎么办?这些 try catch 还可能是级联的,一层一层抛出去。