Java 方法 sqrt 在 Eclipse 中未定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23049145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method sqrt is undefined in eclipse?
提问by ArcaneMusic
I'm working on a class for a school assignment where we're trying to use interfaces/comparables, and a root class called shape is being used to define other shape classes such as rectangles, circles, trapezoids, ect. The problem I'm running into is the use of the Square root method and the Power method when trying to make the trapezoid class.
我正在为我们正在尝试使用接口/可比对象的学校作业编写一个类,并且使用一个名为 shape 的根类来定义其他形状类,例如矩形、圆形、梯形等。我遇到的问题是在尝试制作梯形类时使用 Square root 方法和 Power 方法。
What I'm trying to do is to get the 3rd and 4th side of a trapezoid, and because these are all going to be regular trapezoids, I can take the variable side1 (The top side of the trapezoid), subtract that from side2 (The bottom side of the trapezoid), divide it by two s that each side has the bottom half of the right triangle I'm trying to create, and then do the Pythagorean theorem to get that pesky 3rd side.
我想要做的是得到梯形的第 3 和第 4 边,因为这些都是常规梯形,我可以取变量 side1(梯形的顶边),从 side2 减去它(梯形的底边),将其除以两个 s,每条边都有我试图创建的直角三角形的下半部分,然后执行勾股定理得到那个讨厌的第三边。
The problem is that every time I'm trying to run the Square root (sqrt) or power (pow) method to preform this, eclipse is giving me an error saying that "The method sqrt(double) is undefined for the type Trapezoid" I have no clue what I'm missing in this, so any help would be appreciated.
问题是,每次我尝试运行平方根 (sqrt) 或幂 (pow) 方法来执行此操作时,eclipse 都会给我一个错误,指出“方法 sqrt(double) 对于梯形类型未定义”我不知道我在这方面缺少什么,所以任何帮助将不胜感激。
The code I have is as follows:
我的代码如下:
import java.lang.Math;
public class Trapezoid implements Shape {
public double side1;
public double side2;
public double height;
public Trapezoid(double side1, double side2, double height){
this.side1 = side1;
this.side2 = side2;
this.height = height;
}
public double perimeter(){ //THIS IS THE METHOD CAUSING PROBLEMS!
double tosser = sqrt((((side2 - side1) / 2 *(side2 - side1) / 2) + (height*height) ) );
return (side1 + side2 + 2 * tosser );
}//end perimeter
public double area(){
return (((side1 + side2) / 2) * height);
}//end area
public String toString(){
return "The area of the trapezoid is" + area() + ".";
}//end toString
public int compareTo(Shape that){
int larger = 0;
if(this.area() > that.area())
larger = 1;
else if(this.area() < that.area())
larger = -1;
return larger;
}
}
Also, the class for shape is rather small as well, but I can't make any changes to it for this assignment:
此外,形状类也相当小,但我无法为此作业对其进行任何更改:
public interface Shape {
public double area();
public double perimeter();
}
采纳答案by Reimeus
You need to statically import the method to use it as an unqualified method
您需要静态导入该方法以将其用作不合格的方法
import static java.lang.Math.sqrt;
or use
或使用
double tosser = Math.sqrt(...);
(importing from java.lang
is unnecessary since those classes are imported by default)
(导入 fromjava.lang
是不必要的,因为这些类是默认导入的)