Java编写一个测试程序,提示用户输入三角形的三边、颜色和三角形是否是实心的布尔值,之后创建一个三角形对象,并使用输入设置颜色和填充属性。该程序应显示面积、周长、颜色以及实心与否等信息。输入条件不满足三角形属性则输出“失败”
时间: 2024-05-12 08:16:40 浏览: 23
好的,您的问题是如下:
```java
import java.util.Scanner;
public class TriangleTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入三角形的三条边长:");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
System.out.print("请输入三角形的颜色:");
String color = input.next();
System.out.print("请输入三角形是否是实心的(true/false):");
boolean isFilled = input.nextBoolean();
if (isValidTriangle(side1, side2, side3)) {
Triangle triangle = new Triangle(side1, side2, side3);
triangle.setColor(color);
triangle.setFilled(isFilled);
System.out.println("三角形的面积为:" + triangle.getArea());
System.out.println("三角形的周长为:" + triangle.getPerimeter());
System.out.println("三角形的颜色为:" + triangle.getColor());
System.out.println("三角形是否是实心的:" + triangle.isFilled());
} else {
System.out.println("失败");
}
}
public static boolean isValidTriangle(double side1, double side2, double side3) {
return (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1);
}
}
class Triangle {
private double side1;
private double side2;
private double side3;
private String color;
private boolean filled;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
public double getPerimeter() {
return side1 + side2 + side3;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
@Override
public String toString() {
return "Triangle: side1 = " + side1 + ", side2 = " + side2 + ", side3 = " + side3;
}
}
```
阅读全文