java 中Math.sqrt()使用注意事项 Type mismatch: cannot convert from double to float
2017-10-10 21:30
726 查看
若使用Math.sqrt() 出现Type mismatch: cannot convert from double to float
那是因为Math.sqrt()返回正确舍入的一个double值的正平方根,若你将值赋值给不是double类型的变量,将会出现以上问题。
例子:
public class Main {
public static void main(String[] args) {
float[] a = new
float[6];
float[] s = new
float[3];
System.out.println("Enter three points for a triangle:");
Scanner tri = new Scanner(System.in);
for(int i=0; i<6; i++) {
a[i] = tri.nextFloat();
}
s[0] = Math.sqrt(Math.pow(Math.abs(a[0]-a[2]), 2)+Math.pow(Math.abs(a[1]-a[3]), 2));
s[1] = Math.sqrt(Math.pow(Math.abs(a[0]-a[4]), 2)+Math.pow(Math.abs(a[1]-a[5]), 2));
s[2] = Math.sqrt(Math.pow(Math.abs(a[2]-a[4]), 2)+Math.pow(Math.abs(a[3]-a[5]), 2));
System.out.println(s[0]);
System.out.println(s[1]);
System.out.println(s[2]);
}
}
这个将会出错,因为接受Math.sqrt返回值的变量是float变量
正确的是
public class Main {
public static void main(String[] args) {
double[] a = new
double[6];
double[] s = new
double[3];
System.out.println("Enter three points for a triangle:");
Scanner tri = new Scanner(System.in);
for(int i=0; i<6; i++) {
a[i] = tri.nextFloat();
}
s[0] = Math.sqrt(Math.pow(Math.abs(a[0]-a[2]), 2)+Math.pow(Math.abs(a[1]-a[3]), 2));
s[1] = Math.sqrt(Math.pow(Math.abs(a[0]-a[4]), 2)+Math.pow(Math.abs(a[1]-a[5]), 2));
s[2] = Math.sqrt(Math.pow(Math.abs(a[2]-a[4]), 2)+Math.pow(Math.abs(a[3]-a[5]), 2));
System.out.println(s[0]);
System.out.println(s[1]);
System.out.println(s[2]);
}
}
那是因为Math.sqrt()返回正确舍入的一个double值的正平方根,若你将值赋值给不是double类型的变量,将会出现以上问题。
例子:
public class Main {
public static void main(String[] args) {
float[] a = new
float[6];
float[] s = new
float[3];
System.out.println("Enter three points for a triangle:");
Scanner tri = new Scanner(System.in);
for(int i=0; i<6; i++) {
a[i] = tri.nextFloat();
}
s[0] = Math.sqrt(Math.pow(Math.abs(a[0]-a[2]), 2)+Math.pow(Math.abs(a[1]-a[3]), 2));
s[1] = Math.sqrt(Math.pow(Math.abs(a[0]-a[4]), 2)+Math.pow(Math.abs(a[1]-a[5]), 2));
s[2] = Math.sqrt(Math.pow(Math.abs(a[2]-a[4]), 2)+Math.pow(Math.abs(a[3]-a[5]), 2));
System.out.println(s[0]);
System.out.println(s[1]);
System.out.println(s[2]);
}
}
这个将会出错,因为接受Math.sqrt返回值的变量是float变量
正确的是
public class Main {
public static void main(String[] args) {
double[] a = new
double[6];
double[] s = new
double[3];
System.out.println("Enter three points for a triangle:");
Scanner tri = new Scanner(System.in);
for(int i=0; i<6; i++) {
a[i] = tri.nextFloat();
}
s[0] = Math.sqrt(Math.pow(Math.abs(a[0]-a[2]), 2)+Math.pow(Math.abs(a[1]-a[3]), 2));
s[1] = Math.sqrt(Math.pow(Math.abs(a[0]-a[4]), 2)+Math.pow(Math.abs(a[1]-a[5]), 2));
s[2] = Math.sqrt(Math.pow(Math.abs(a[2]-a[4]), 2)+Math.pow(Math.abs(a[3]-a[5]), 2));
System.out.println(s[0]);
System.out.println(s[1]);
System.out.println(s[2]);
}
}
相关文章推荐
- 使用Junit出现Type mismatch: cannot convert from Test to Annotation
- 使用Junit出现Type mismatch: cannot convert from Test to Annotation
- Type mismatch: cannot convert from java.sql.PreparedStatement to com.mysql.jdbc.PreparedStatement
- Type mismatch: cannot convert from Integer to int(java 报错)
- Type mismatch: cannot convert from java.sql.Connection to com.sun.。。。问题的解决
- Jdbc-Type mismatch: cannot convert from java.sql.ResultSet to com.mysql.jdbc.ResultSet
- junit Type mismatch: cannot convert from Test to Annotation
- Id Type mismatch: cannot convert from connection to connection
- Type mismatch: cannot convert from MainFragment to Fragment 报错
- 解决Java“type mismatch cannot convert from byte[] to string”问题
- Type mismatch: cannot convert from Test to Annotation
- Type mismatch: cannot convert from MainFragment to Fragment 报错
- Hibernate Type mismatch: cannot convert from CascadeType to CascadeType[]
- 报错信息:Type mismatch: cannot convert from org.hibernate.Transaction to javax.transaction.Transaction
- Type mismatch: cannot convert from Object to Member
- Type mismatch: cannot convert from Test to Annotation
- Type mismatch: cannot convert from Test to Annotation
- Type mismatch: cannot convert from Object to String
- Type mismatch: cannot convert from Test to Annotation
- 由"Type mismatch: cannot convert from View to ProgressBar"联想到的类型转换错误的根源