无法在基本类型double上调用sqrt(double)

时间:2016-09-18 18:21:10

标签: java math sqrt

由于某种原因,我的代码不会运行我得到错误“无法在原始类型double上调用sqrt(double)”这是我编译时得到的唯一错误,所以其他一切都不会导致错误,除了数学。 sqrt(数学)线。帮助

public class Confused {

public static double average(int x, int y) {
double ave= (x+y)/2.0;
return ave;
}
public static double slope(int x1, int x2, int y1, int y2){
double slope= (y2-y1)/(x2-x1);
return slope; }

public static int difference(int x, int y) {
int diff=x-y;
return diff; }

public static int square(int x) {
int power1=(int)Math.pow(x, 2);
return power1;

}
 public static double distance (int x1, int x2, int y1, int y2) {
double Math= (square(difference(x2,x1))) + (square(difference(y2,y1)));
 double dist= Math.sqrt(Math);
return dist;

}

public static void main(String[] args) { 
  System.out.println(average(2,3));
  System.out.println(slope(1,2,3,4));
  System.out.println(difference(10,5));
  System.out.println(square(10));
  System.out.println(distance(2,3,6,8));

}

}

3 个答案:

答案 0 :(得分:3)

double Math= (square(difference(x2,x1))) + (square(difference(y2,y1)));
double dist= Math.sqrt(Math);

您正在创建一个新变量Math,在您引用该新变量的下一行,因为它会影响全局类Math。所以它试图调用不存在的double.sqrt(double)

答案 1 :(得分:1)

通过将其用作变量,您是hiding内置类double result = square(difference(x2, x1)) + square(difference(y2, y1)); return Math.sqrt(result); 。你可以做到

conn.getOutputStream()

答案 2 :(得分:0)

因为Math是Java中的类,所以不能将其用作对象变量。而是使用其他一些变量名称:

double operation = (square(difference(x2,x1))) + (square(difference(y2,y1)));
double dist = Math.sqrt(operation);