三角测试仪

时间:2016-10-31 20:02:59

标签: java java.util.scanner math.sqrt

我已经创建了一个Triangle类程序,但我需要一个测试器类。在测试器类中,它必须要求用户输入点,然后它将计算边,因此第四。如何让用户在测试仪中输入x1和y1,然后在三角形类中获取数据?顺便说一下,我需要使用Scanner。

import java.util.Scanner;

public class Triangle {

    private double x1;
    private double y1;
    private double x2;
    private double y2;
    private double x3;
    private double y3;
    private double side1;
    private double side2;
    private double side3;
    private double angleA;
    private double angleB;
    private double angleC;

    public Triangle(double a1, double a2, double b1, double b2, double c1, double c2){
    x1=a1;
    y1=a2;
    x2=b1;
    y2=b2;
    x3=c1;
    y3=c2;
    }

    public double getSide1(){
        side1 = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
        return side1;
    }
    public double getSide2(){
        side2 = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2));
        return side2;
    }
    public double getSide3(){
        side3 = Math.sqrt(Math.pow(x3-x1,2)+Math.pow(y3-y1,2));
        return side3;
    }
    public double getAngleA(){
        angleA = side1 + side2 + side3 - (side2 * side3);
        return angleA;
    }
    public double getAngleB(){
        angleB = side2 + side1 + side3 - (side1 * side3);
        return angleA;
    }
    public double getAngleC(){
        angleC = side3 + side1 + side2 - (side1 * side2);
        return angleA;
    }

}

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式使用Scanner课程:

....

private static Scanner input;

public static void main(String[] args) {
    input = new Scanner(System.in);

    //... Read x1 from the console.
    System.out.print("Enter x1 : ");
    int x1 = input.nextInt();

    //... Read y1 from the console.
    System.out.print("Enter y1: ");
    int y1 = input.nextInt();

    System.out.print("x1=" + x1 + ", y1=" + y1);

}
....