我正在研究一个项目,该项目计算三角形的边界和面积,给出三边a,b,c,并且边b是三角形的底边,我已经为它做了一个类,如下所示但是我在设置数学时遇到问题,我猜测试器文件是否全部有效,如果有人能帮助我,我将非常感激
编辑:我做了一些更正(非大写类似乎有些帮助)至于构造函数我想要一个处理所有三个方面并且不确定如何处理它在编译时我遇到了一些我无法弄清楚的错误(这里很晚)并且不确定如何发布我的错误
// Class declaration
public class Triangle
{
// 3 private variables for the 3 triangle sides.
private double sideA;
private double sideB;
private double sideC;
// constant value for bad input.
public static final int INVALID_DIMENSION = -1;
// one arg constructor. Divides input by 3 and calls
// setter methods.
public Triangle(double length)
{
int sideLength = length/3;
setSideA(sideLength);
setSideB(sideLength);
setSideC(sideLength);
}
// setter for sideA
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideA(double a)
{
if (a > 0)
this.sideA = a;
else
this.sideA = INVALID_DIMENSION;
}
// setter for sideB
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideB(double b)
{
if (b > 0)
this.sideB = b;
else
this.sideB = INVALID_DIMENSION;
}
//setter for sideC
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideC(double c)
{
if (c > 0)
this.sideC = c;
else
this.sideC = INVALID_DIMENSION;
}
// private method that cheks to see if any values have been assigned INVALID_DIMENSION. If any have been assigned
// that value, the method returns false. If all values are valid the method returns true.
private boolean isValid()
{
if((this.sideA == INVALID_DIMENSION)||(this.sideB == INVALID_DIMENSION)||(this.sideC == INVALID_DIMENSION))
return false;
else
return true;
}
// checks to see if all values are valid. If all values pass then Area is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned.
public double getArea()
{
if (isValid())
{
// Logic here
return // return new value here.
}
return INVALID_DIMENSION;
}
// checks to see if all values are valid. If all values pass then Area is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned.
public double getPermiter()
{
if (isValid())
{
// Logic here
return // return new value here.
}
return INVALID_DIMENSION;
}
}
编辑大家好!对不起延迟我接受了你们的建议并一点一点地把它拿走了,我整理出了我现在添加数学的错误,但我遇到了一个错误,说“无法达到的声明”,我无法弄清楚为什么这里是新的新代码,其中包含错误弹出位置的注释
// Class declaration
public class Triangle
{
// 3 private variables for the 3 triangle sides.
private double sideA;
private double sideB;
private double sideC;
private double s;
private double area;
// constant value for bad input.
public static final int INVALID_DIMENSION = -1;
// one arg construcotr. Divides input by 3 and calls
// setter methods.
public Triangle(double length)
{
double sideLength = length/3;
setSideA(sideLength);
setSideB(sideLength);
setSideC(sideLength);
}
// setter for sideA
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideA(double a)
{
if (a > 0)
this.sideA = a;
else
this.sideA = INVALID_DIMENSION;
}
// setter for sideB
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideB(double b)
{
if (b > 0)
this.sideB = b;
else
this.sideB = INVALID_DIMENSION;
}
//setter for sideC
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideC(double c)
{
if (c > 0)
this.sideC = c;
else
this.sideC = INVALID_DIMENSION;
}
// private method that checks to see if any values have been assigned INVALID_DIMENSION. If any have been assigned
// that value, the method returns false. If all values are valid the method returns true.
private boolean isValid()
{
if((this.sideA == INVALID_DIMENSION)||(this.sideB == INVALID_DIMENSION)||(this.sideC == INVALID_DIMENSION))
return false;
else
return true;
}
// checks to see if all values are valid. If all values pass then Area is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned.
public double getArea()
{
if (isValid())
{
// Logic here
s = 1/2 * (sideA + sideB + sideC);
area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));
return getArea(); // return new value here.
}
else
{
return INVALID_DIMENSION;
}
} // checks to see if all values are valid. If all values pass then permiter is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned.
public double getPermiter()
{
if (isValid())
{
// Logic here
double s = (sideA+sideB+sideC)/2;
return Math.sqrt(s * (s-sideA) * (s-sideB) * (s-sideC));
return getPermiter(); // return new value here. this is where it is telling me where the error is
}
return INVALID_DIMENSION;
}
}
答案 0 :(得分:0)
似乎你在同一种方法中混合了两种功能。
如果您发现有两个退货声明。摆脱第二个。它无法访问,因为该方法在返回语句之前退出。
return Math.sqrt(s * (s-sideA) * (s-sideB) * (s-sideC));
return getPermiter();
如果您将此部分重构为如下所示:
public double getPermiter(){
if(!isValid()){
throw new InvalidTriangleException("your trangle isn't a trangle dood");
}
double s = (sideA+sideB+sideC)/2;
return Math.sqrt(s * (s-sideA) * (s-sideB) * (s-sideC));
}
你不必依赖调用者知道该函数如何处理INVALID_DEMENSION案例,并且任何错误都会很快出现。
----定义自定义异常 How to define custom exception class in Java, the easiest way?
但基本上,
public InvalidTriangleException extends Exception{
public InvalidTriangleException(String message){
super(message)
}
}