package day01;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ShapeTest shapeTest = new ShapeTest();
while (sc.hasNext()){
Shape shape = null;
String StrData = sc.nextLine();
//System.out.println(strData);
String[] arrayData = StrData.split(" ");
/*
* split() 方法是 Java 字符串类的一个方法,
* 用于将字符串分割为子字符串,并返回一个字符串数组。
* public String[] split(String regex)
* regex:分隔符,可以是一个正则表达式。
* 输出:字符串数组,包含分割后的子字符串。
* */
int count = arrayData.length;
if(count==1) {
//该行只有一个参数,构造原形
double r = Double.parseDouble(arrayData[0]);
/*用Double.parseDouble()是把括号里面内容变成double类型的。
其中括号中的参数必须是String类型,否则转换会报错。*/
shape = new Circle(r);
}
else if(count==2) {
//该行有2个参数,构造长方形
double length = Double.parseDouble(arrayData[0]);
double width = Double.parseDouble(arrayData[1]);
shape = new Rectangle(length,width);
}
else if(count==3) {
//该行有3个参数,构造三角形
double a = Double.parseDouble(arrayData[0]);
double b = Double.parseDouble(arrayData[1]);
double c = Double.parseDouble(arrayData[2]);
shape = new Triangle(a, b, c);
}
if (shape!=null) {
double area = shapeTest.length(shape);
System.out.printf("%.2f\n",area);
}
else {
System.out.println("0.00");
}
}
}
}
interface Shape{
public double length();
}
class Triangle implements Shape{
private double a;
private double b;
private double c;
public Triangle(double a,double b,double c){
if(a<0||b<0||c<0){
a = 0;
b = 0;
c = 0;
}
if(a+b>c&&a+c>b&&b+c>a){
//能构成三角形就赋值
{
this.a = a;
this.b = b;
this.c = c;
}
}
}
public double length(){
return a+b+c;
}
}
class Rectangle implements Shape{
private double l;
private double h;
public Rectangle(double l,double h){
if(l>0&&h>0){
this.l = l;
this.h = h;
}
}
public double length(){
return 2*(l+h);
}
}
class Circle implements Shape{
double r;
public Circle(double r){
if(r>=0){
this.r = r;
}
}
@Override
public double length() {
return 3.14*2*r;
}
}
class ShapeTest{
public double length(Shape shape){
return shape.length();
}
}
05-03
04-30
3030
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)