关于#代码#的问题,如何解决?


import java.util.Scanner;

interface Shape {
    public double length();
}

class Triangle implements Shape {
    double a, b, c;

    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double length() {
        if ((a > 0 && b > 0 && c > 0) && (a + b > c || a + c > b || b + c > a))
            return a + b + c;
        else
            return 0;
    }
}

class Rectangle implements Shape {
    double a, b;

    public Rectangle(double a, double b) {
        this.a = a;
        this.b = b;
    }

    public double length() {
        if (a > 0 && b > 0)
            return 2 * (a + b);
        else
            return 0;
    }
}

class Circle implements Shape {
    double r;

    public Circle(double r) {
        this.r = r;
    }

    public double length() {
        if (r > 0)
            return 2 * 3.14 * r;
        else
            return 0;
    }
}

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner reader = new Scanner(System.in);
        String str;
        int n, m, t;
        while (reader.hasNext()) {
            str = reader.nextLine();
            String s[] = str.split(" ");
            if (s.length == 1) {
                n = Integer.parseInt(s[0]);
                Circle circle = new Circle(n);
                System.out.printf("%.2f\n", circle.length());
            } else if (s.length == 2) {
                n = Integer.parseInt(s[0]);
                m = Integer.parseInt(s[1]);
                Rectangle rectangle = new Rectangle(n, m);
                System.out.printf("%.2f\n", rectangle.length());
            } else if (s.length == 3) {
                n = Integer.parseInt(s[0]);
                m = Integer.parseInt(s[1]);
                t = Integer.parseInt(s[2]);
                Triangle triangle = new Triangle(n, m, t);
                System.out.printf("%.2f\n", triangle.length());

            }
        }
    }

}

img

img

img

这个代码哪里出问题了

是不是测试类名不对,题目要求的是ShapeTest。
另外题目也没明确不能构成三角形的时候是否应该输出周长

if ((a > 0 && b > 0 && c > 0) && (a + b > c || a + c > b || b + c > a))
这里应该是
if ((a > 0 && b > 0 && c > 0) && (a + b > c && a + c > b && b + c > a))