修改代码,使代码正常运行:public interface triangle { double getPerimeter(double a, double b, double c); double getArea(double a, double b, double c); } class Perimeter implements triangle { public double getPerimeter(double a,double b,double c) { double perimeter = 0; perimeter = (a+b+c); return perimeter; } } class Area implements triangle { public double getArea(double a,double b,double c) { double area = 0; double p = 0; p = (a+b+c)/2; area = Math.sqrt(p*(p-a)*(p-b)*(p-c)); return area; } } public class Output { public static void main(String[] args) { double a = 3, b = 4, c = 5; Perimeter C = new Perimeter(); Area S = new Area(); System.out.println("三角形的周长为:"+C); System.out.println("三角形的面积为:"+S); } }
时间: 2024-03-11 07:46:50 浏览: 164
修改后的代码如下:
```
public interface Triangle {
double getPerimeter(double a, double b, double c);
double getArea(double a, double b, double c);
}
class Perimeter implements Triangle {
public double getPerimeter(double a, double b, double c) {
double perimeter = 0;
perimeter = (a + b + c);
return perimeter;
}
}
class Area implements Triangle {
public double getArea(double a, double b, double c) {
double area = 0;
double p = 0;
p = (a + b + c) / 2;
area = Math.sqrt(p * (p - a) * (p - b) * (p - c));
return area;
}
}
public class Output {
public static void main(String[] args) {
double a = 3, b = 4, c = 5;
Perimeter c = new Perimeter();
Area s = new Area();
System.out.println("三角形的周长为:" + c.getPerimeter(a, b, c));
System.out.println("三角形的面积为:" + s.getArea(a, b, c));
}
}
```
主要修改如下:
1. 将 `triangle` 修改为 `Triangle`,符合 Java 命名规范;
2. 将 `Perimeter C` 和 `Area S` 修改为 `Perimeter c` 和 `Area s`,符合 Java 命名规范;
3. 在 `main` 方法中,将 `System.out.println` 中的 `C` 和 `S` 改为 `c.getPerimeter(a, b, c)` 和 `s.getArea(a, b, c)`,以调用 `Perimeter` 和 `Area` 类中的方法,获取计算结果。
阅读全文