我的代碼如下
//Test.java
import?java.util.Scanner;
public?class?Test?{
/**
?*?@param?args
?*/
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
Scanner?input?=?new?Scanner(System.in);
System.out.println("pls?input?the?r?of?circle");
float?r?=?input.nextFloat();
System.out.println("pls?input?the?length?of?rectangle");
float?length?=?input.nextFloat();
System.out.println("pls?input?the?width?of?rectangle");
float?width?=?input.nextFloat();
Circle?circle?=?new?Circle(r);
Rectangle?rectangle?=?new?Rectangle(length,width);
System.out.println("圓形的周長為"?+?circle.perimeter());
System.out.println("圓形的面積為"?+?circle.square());
System.out.println("長方形的周長為"?+?rectangle.perimeter());
System.out.println("長方形的面積為"?+?rectangle.square());
}
}Shape.java
public?abstract?class?Shape?{
public?abstract?float?square();
public?abstract?float?perimeter();
}Circle.java
public?class?Circle?extends?Shape{
float?r;
public?Circle(float?r0){
r?=?r0;
}
@Override
public?float?perimeter()?{
//?TODO?Auto-generated?method?stub
return?2*r*(float)Math.PI;
}
@Override
public?float?square()?{
//?TODO?Auto-generated?method?stub
return?(float)Math.PI*r*r;
}
}Rectangle.java
public?class?Rectangle?extends?Shape?{
public?float?length;
public?float?width;
public?Rectangle(float?length0,float?width0){
width?=?width0;
length?=?length0;
if(length?<=?0){
System.out.println("輸入的長度<=0,有誤");
}
if(width?<=?0){
System.out.println("輸入的寬度<=0,請重新輸入");
}
}
@Override
public?float?perimeter()?{
//?TODO?Auto-generated?method?stub
return?2*(length?+?width);
}
@Override
public?float?square()?{
//?TODO?Auto-generated?method?stub
return?length?*?width;
}
}
2015-12-28
??if(length?<=?0){ ????????????????System.out.println("輸入的長度<=0,有誤"); ????????} ????????if(width?<=?0){ ????????????????System.out.println("輸入的寬度<=0,請重新輸入");這段,怎么實現(xiàn)輸入錯誤之后的重新輸入?
2015-08-07
寫的非常好 贊一個