gpt4 book ai didi

java - 求三角形的长度和角度

转载 作者:行者123 更新时间:2023-12-01 22:36:20 25 4
gpt4 key购买 nike

我的工作是计算给定三角形的以下属性:所有边的长度、所有角的角度、周长和面积。我有一个三角形类和一个三角形测试器类。我认为我已经正确编码了周长和面积?但我开始思考,我不应该为我的周长设置一个恒定的可变边,而应该使用所有边的长度来找到我的周长。我所坚持的是找到长度和角度。由于某种原因,当我运行测试程序类时,它们都以 1.0 的形式出现。任何意见,将不胜感激!谢谢!

    import java.util.Scanner;

public class Triangle
{
Scanner in = new Scanner(System.in);

/**
* Variables
*/
double x1;
double x2;
double x3;
double y1;
double y2;
double y3;
double lengthA;
double lengthB;
double lengthC;
double angleA;
double angleB;
double angleC;
double area;
double perimeter;
double base;
double height;
double p;

/**
Constructs x and y coordinates
@param x1, x2, x3 to x1, x2, x3
@param y1, y2, y3 to y1, y2, y3
*/
public Triangle(double x1, double x2, double x3, double y1, double y2, double y3)
{
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
}

/**
*Find lengths of all sides
*/
public double getLengthA()
{
lengthA = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
return lengthA;
}

public double getLengthB()
{
lengthB = Math.sqrt(Math.pow((x3 - x2), 2) + Math.pow((y3 - y2), 2));
return lengthB;
}

public double getLengthC()
{
lengthC = Math.sqrt(Math.pow((x1 - x3), 2) + Math.pow((y1 - y3), 2));
return lengthC;
}

/**
* Find angles at all corners
@return angles at all corners
*/
public double getAngleA()
{
angleA = lengthA + lengthB + lengthC - (lengthB * lengthC);
return angleA;
}

public double getAngleB()
{
angleB = lengthB + lengthA + lengthC - (lengthA * lengthC);
return angleB;
}

public double getAngleC()
{
angleC = lengthC + lengthA + lengthB - (lengthA * lengthB);
return angleC;
}


/**
* Constant Variables
*/
public Triangle()
{
base = 5;
height = 15;
}

/**
* Find perimeter of triangle
*/
public double getPerimeter()
{
perimeter = lengthA + lengthB + lengthC;
return perimeter;
}

public double getHalfPerimeter()
{
p = perimeter / 2;
return p;
}

/**
* Find area of triangle
*/
public double getArea()
{
double area = Math.sqrt(p * (p - lengthA) * (p - lengthB) * (p - lengthC));
return area;
}
}

这是我的测试人员类(class):

    import java.util.Scanner;

import javax.swing.JOptionPane;


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


String input = JOptionPane.showInputDialog("Enter X coordinate for the first corner of the triangle: ");
double x1 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the first corner of the triangle: ");
double y1 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter X coordinate for the second corner of the triangle: ");
double x2 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the second corner of the triangle: ");
double y2 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter X coordinate for the third corner of the triangle: ");
double x3 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the third corner of the triangle: ");
double y3 = Double.parseDouble(input);

Triangle t = new Triangle(x1, x2, x3, y1, y2, y3);

System.out.println("Length A is: " + t.getLengthA());
System.out.println("Length B is: " + t.getLengthB());
System.out.println("Length C is: " + t.getLengthC());
System.out.println("Angle A is: " + t.getAngleA());
System.out.println("Angle B is: " + t.getAngleB());
System.out.println("Angle C is: " + t.getAngleC());
System.out.println("Area: " + t.getArea());
System.out.println("Perimeter: " + t.getPerimeter());

in.close();
}
}

最佳答案

您正在显示提示用户输入的 JOptionPanes,但您没有获得输入,因此您没有使用输入来设置三角形的状态,而是创建了一个默认值使用其无参数构造函数的三角形对象。要了解 Java 编程中没有魔法,并且您的 Triangle 对象不会神奇地知道用户输入了哪些数字并相应地更改自身。相反,必须将该信息输入到您的三角形中。

您必须做的是分配从 JOptionPanes 返回的结果,将它们解析为 double ,然后使用这些数字创建一个 Triangle,使用采用数值参数的构造函数,而不是默认构造函数。这样做,你应该会很好。

例如

String input = JOptionPane.showInputDialog("Enter X coordinate for the first corner of the triangle: ");
double x1 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the first corner of the triangle:
double y1 = Double.parseDouble(input);
//.... etc repeat...

Triangle triangle = new Triangle(x1, x2, x3, y1, y2, y3);
<小时/>

编辑

此构造函数忽略传入的值:

公共(public)三角形(双x1,双x2,双x3,双y1,双y2,双y3){ x1=0; x2 = 0; x3 = 0; y1=0; y2=0; y3=0;}

像您需要的那样的构造函数应该采用传入的值,并使用这些值来设置类字段。例如这里:

public class Foo {
private int bar; // the bar field

public Foo(int bar) {
this.bar = bar;
}
}

请注意,我在上面使用了 this.bar ,以便 Java 知道我想要使用 bar 参数保存的值来设置 bar 字段(条形没有 这个)。您只需使用 6 个参数(而不是一个)执行类似的操作。

然后,您可以在称为初始化程序 block 的单独代码块中进行所有计算:

{ 
/**
* Find lengths of all sides
*/
lengthA = Math.pow(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2) * .05, lengthA);
lengthB = Math.pow(Math.pow((x3 - x2), 2) + Math.pow((y3 - y2), 2) * .05, lengthB);
lengthC = Math.pow(Math.pow((x1 - x3), 2) + Math.pow((y1 - y3), 2) * .05, lengthC);
}

此代码在构造函数之前被调用,因此即使您正确设置了 Triangles 字段,此代码也无法工作。您不想使用初始化 block ,您可以忘记我什至提到过它们,而不是告诉您不要使用它们。相反,在构造函数内进行计算,并设置所有字段后进行计算。

请注意,我故意没有发布您问题的解决方案,因为我坚信我们大多数人需要的是理解我们所遇到的任何问题背后的概念,然后利用这种理解来创建我们自己的代码解决方案。

最重要的是,阅读你的文本,没有更好,研究你的文本,因为你所犯的错误涉及基本概念,并表明你尚未理解这些概念并诉诸于猜测。这永远行不通,因为如果您想在本类(class)中取得进展,您需要理解所有这些,并且要充分理解它。

祝你好运!

<小时/>

编辑2
对于 Tom,运行以下命令:

public class TestInitializerBlock {
public TestInitializerBlock() {
System.out.println("Inside of constructor");
}

{
System.out.println("Inside of initializer block");
}

public static void main(String[] args) {
new TestInitializerBlock();
}
}

关于java - 求三角形的长度和角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26819957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com