我对此比较陌生,任何帮助都会很棒。对于我的作业,我应该制作一个Triangle和TriangleTester类,使用扫描仪操作值,以找到三角形边,角度,周长等的长度。我不是要求你做我的作业但是可能指向我在正确的方向。这只是一小部分,但它让我无法继续。
我无法弄清楚的是如何让我输入的变量跨越方法(如果这是有道理的。
这是我的三角代码:
import java.math.*;
import java.util.Scanner;
public class Triangle
{
private double A, B, C, D, h;
public double x1, x2, x3, y1, y2, y3;
public double lengthA, lengthB, lengthC;
private double angleA, angleB, angleC;
private double calcPerimeter, calcArea, getHeight;
//Create new Triangle
public Triangle(){
}
//to get length of a (AB) use A = (x1, y1) & B = (x2,y2)
public double getLengthC(){
double distance = Math.sqrt((Math.pow((x1-x2),2))+(Math.pow((y1-y2),2)));
return distance;
}
}
这是我的TriangleTester代码:
import java.util.Scanner;
public class TriangleTester
{
public static void main(String[]args)
{
Triangle triangle1 = new Triangle();
Scanner scan = new Scanner(System.in);
System.out.println("Enter x1 for point A");
double x1 = scan.nextDouble();
System.out.println("Enter y1 for point A");
double y1 = scan.nextDouble();
System.out.println("Enter x2 for point A");
double x2 = scan.nextDouble();
System.out.println("Enter y2 for point A");
double y2 = scan.nextDouble();
System.out.println("Enter x3 for point A");
double x3 = scan.nextDouble();
System.out.println("Enter y3 for point A");
double y3 = scan.nextDouble();
System.out.println("Set up a Triangle with coordinates " +"("+x1+","+y1+")"+ ","+ "("+x2+","+y2+")" +","+ "("+x3+","+y3+")");
System.out.println("Length of side c " + triangle1.getLengthC());
}
}
出于测试目的,我想输入值:
0
0
4
0
3
3
并且将c的长度计算为4.我看到当我在TriangleTester类中使用变量时,变量可以工作,但是当我从Triangle类调用一个方法时,值不起作用。
从我所在的地方,我认为我必须操纵三角形类,以便我将来自扫描仪的输入移动到该区域......或者我可能是错的
public Triangle(){} //I would have to add something here maybe?
答案 0 :(得分:0)
TriangleTester
中获得了用户输入,因此需要将其传递给Triangle
类。
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;
}
在从用户获取值之后创建Triangle
对象:
Triangle triangle1 = new Triangle(x1, x2, x3, y1, y2, y3);
System.out.println("Length of side c " + triangle1.getLengthC());
注意:建议您将班级的所有实例变量设为private
,并为其提供正确的 getter和setter 。
答案 1 :(得分:0)
Scanner
中的问题不是。您永远不会填写Triangle triangle1
变量中的字段,因此所有字段都有默认值,即0.0。
刚开始从变量填充字段。例如:
triangle1.x1 = x1;
//and on...
但这是一个不好的做法,最好将字段声明为private
并使用setter作为作业,或者使用接受必要字段的构造函数。
class Triangle {
private double x1, x2, x3, y1, y2, y3;
public double getX1() {
return this.x1;
}
public void setX1(double x1) {
this.x1 = x1;
}
//other fields, getters and setters...
}
//...
//in your TriangleTester#main method
triangle1.setX1(x1);
//same for other fields...
答案 2 :(得分:0)
您尚未使用从triangle
Scanner
的值
这可以作为构造函数的一部分来完成:
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;
}
或者您可以在Traingle类上设置setter方法
void setX1 (double x1)
{
this.x1 = x1;
}
etc.
答案 3 :(得分:-4)
试试这个
import java.util.Scanner;
公共类MainClass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter x1 for point A");
double x1 = scan.nextDouble();
System.out.println("Enter y1 for point A");
double y1 = scan.nextDouble();
System.out.println("Enter x2 for point A");
double x2 = scan.nextDouble();
System.out.println("Enter y2 for point A");
double y2 = scan.nextDouble();
System.out.println("Enter x3 for point A");
double x3 = scan.nextDouble();
System.out.println("Enter y3 for point A");
double y3 = scan.nextDouble();
System.out.println("Set up a Triangle with coordinates " +"("+x1+","+y1+")"+ ","+ "("+x2+","+y2+")" +","+ "("+x3+","+y3+")");
Triangle triangle1 = new Triangle(x1,x2,y1,y2,x3,y3);
System.out.println("Length of side c " + triangle1.getLengthC());
}
}
和
import java.math.*;
import java.util.Scanner;
public class Triangle
{
private double A, B, C, D, h;
public double x1, x2, x3, y1, y2, y3;
public double lengthA, lengthB, lengthC;
private double angleA, angleB, angleC;
private double calcPerimeter, calcArea, getHeight;
//Create new Triangle
public Triangle(double x1,double x2,double y1,double y2,double x3, double y3){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.x3=x3;
this.y3=y3;
}
//to get length of a (AB) use A = (x1, y1) & B = (x2,y2)
public double getLengthC(){
double distance = Math.sqrt((Math.pow((x1-x2),2))+(Math.pow((y1-y2),2)));
System.out.println(distance);
return distance;
}
}