我的程序仅使用 class GeometricObject 中的默认值而不是用户输入

My program is only using the default values from my class GeometricObject instead of the user input

我目前正在学习 java 中的继承。我有一个 class 几何对象:

public class GeometricObject {
    private String color = "white";
    private boolean filled = false;

    /**Default construct*/
    public GeometricObject() {
    }

    /**Construct a geometric object*/
    public GeometricObject(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
    }

    /**Getter method for color*/
    public String getColor() {
        return color;
    }

    /**Setter method for color*/
    public void setColor(String color) {
        this.color = color;
    }

    /**Getter method for filled. Since filled is boolean,
     so, the get method name is isFilled*/
    public boolean isFilled() {
        return filled;
    }

    /**Setter method for filled*/
    public void setFilled(boolean filled) {
        this.filled = filled;
    }

}

然后我有一个延伸 GemoetricObject 的 class 三角形:

import static java.lang.Math.sqrt;

public class Triangle extends GeometricObject{
    double side1 = 1.0;
    double side2 = 1.0;
    double side3 = 1.0;

    public Triangle(){
        super();
    }

    public Triangle(double side1, double side2, double side3, String color, boolean isFilled){
        super(color,isFilled);
        if (side1 <=0||side2 <= 0||side3 <= 0) {
            System.out.println("Sides must be greater than 0!");
        }

        if (side1 > (side2 + side3)||side2 > (side1 + side3)||side3 > (side1 + side2)) {
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;
            System.out.println("Invalid side lengths!");
        }
    }

    public double getSide1(){
        return side1;
    }

    public double getSide2(){
        return side2;
    }

    public double getSide3(){
        return side3;
    }

    public double getArea(){
        double s = (side1 + side2+ side3) / 2;
        return sqrt(s*(s - side1)*(s - side2)*(s-side3));
    }

    public double getPerimeter(){
        return side1 + side2 + side3;
    }

}

当我转到 运行 程序时,它 运行 没问题。周长和面积都有效。但是,程序没有输出用户输入的值,而是输出 "The white triangle is 1.0 X 1.0 X 1.0." 所以它只使用默认值,但老实说我不确定为什么

编辑:这是我的主要功能,不敢相信我忘记了:

import java.util.Scanner;
public class PA6 {

public static void main(String[] args) {

    Triangle t = new Triangle();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter side 1: ");
    double side1 = input.nextDouble();
    System.out.println("Enter side 2: ");
    double side2 = input.nextDouble();
    System.out.println("Enter side 3: ");
    double side3 = input.nextDouble();
    System.out.println("Enter a color: ");
    String color = input.nextLine();
    input.nextLine();
    System.out.println("Is it filled? ");
    boolean fill = input.nextBoolean();
    input.nextLine();


    System.out.println("The " + t.getColor() + " triangle is " + t.getSide1() + " X " + t.getSide2() + " X " + t.getSide3());
    System.out.println("Area: " + t.getArea());
    System.out.println("Perimeter: " + t.getPerimeter());
    System.out.println("Filled? " + t.isFilled());
}

}

问题在这里:

三角形的每一边都应该比其他两个和小。所以你的 > 看起来应该是正确的。运算符 ||(OR)也应更改为 &&(AND)。

if(side1 < (side2 + side3) && side2 < (side1 + side3) && side3 < (side1 + side2)){ 
    this.side1 = side1;
    this.side2 = side2;
    this.side3 = side3;

} else {
    System.out.println("Invalid side lengths!");
}

更新:

使用next()代替nextLine()

String color = input.next(); 

并删除多余的input.nextLine();

您使用扫描仪从用户输入数据,但您从未将此数据传递给 Triangle 的构造函数。完成用户输入后,只需将这些变量作为参数传递给构造函数:

Triangle t = new Triangle(side1, side2, side3, color, fill);