不兼容的类型:在Java中使用Math.sqrt和Math.pow可能会将Double转换为Int
我得到以下错误,我不知道为什么:( 它显示在代码的第109行。该代码使用Math.sqrt和Math.pow 守则:不兼容的类型:在Java中使用Math.sqrt和Math.pow可能会将Double转换为Int,java,oracle,Java,Oracle,我得到以下错误,我不知道为什么:( 它显示在代码的第109行。该代码使用Math.sqrt和Math.pow 守则: total[b] = Math.sqrt( Math.pow( 2, ( x[b+1][b+1] - x[b][b+1] ) ) + Math.pow( 2, ( x[b+2][b+2] - x[b][b+2] ) ) ); 如果这是一个简单的错误,请恕我直言。我昨天刚开始使用Java,我正在尝试了解它。我也是Stack Overflow的新成员:) 这是我
total[b] = Math.sqrt( Math.pow( 2, ( x[b+1][b+1] - x[b][b+1] ) ) + Math.pow( 2, ( x[b+2][b+2] - x[b][b+2] ) ) );
如果这是一个简单的错误,请恕我直言。我昨天刚开始使用Java,我正在尝试了解它。我也是Stack Overflow的新成员:)
这是我的密码:
import java.util.Scanner;
public class twoDArray4 {
public static void main(String args[])
{
int rows;
int columns = 3;
Scanner scan = new Scanner(System.in);
System.out.print("Please enter the number of cities: ");
rows = scan.nextInt();
double x[][] = new double [rows][columns];
String name[] = new String[rows];
// --- Inputting ---
// 1st City Column Starting from 1
for (int k = 0; k < rows; k++)
{
x[k][0] = (k + 1);
}
for (int i = 0; i < rows; i++)
{
System.out.println(" ");
// Storing City Name
System.out.print("Enter City Name: ");
String names = scan.next();
name[i] = names;
// Storing Coordinates
System.out.println("Enter coordinates for " + name[i] + " by (x [enter] y): ");
for (int j = 1; j < columns; j++)
{
x[i][j] = scan.nextInt();
}
}
// --- Printing ---
// Prints Out: cityName (x, y)
System.out.println(" ");
for (int i = 0; i < rows; i++)
{
System.out.print(" ");
System.out.print(name[i] + " is on (");
for (int j = 1; j < columns; j++)
{
if ( j > 1)
{
System.out.print(", ");
}
System.out.print(x[i][j]);
}
System.out.print(")");
System.out.println(" ");
}
// Prints Out Distance
System.out.println(" ");
System.out.println(" ");
// Factorial Of Rows
int z;
int num = 1;
for(z = 1;z <= rows; z++)
{
num = num * z;
}
int total[] = new int[num];
// Prints Shortest Distance
for (int b = 0; b < num; b++)
{
System.out.print("The shortest distance from " + name[b]);
System.out.println(" to " + name[b+1] + " is ");
total[b] = Math.sqrt( Math.pow( 2, ( x[b+1][b+1] - x[b][b+1] ) ) + Math.pow( 2, ( x[b+2][b+2] - x[b][b+2] ) ) );
}
}
}
import java.util.Scanner;
公共二级Darray4{
公共静态void main(字符串参数[])
{
int行;
int列=3;
扫描仪扫描=新扫描仪(System.in);
System.out.print(“请输入城市编号:”);
rows=scan.nextInt();
双x[][]=新的双[行][列];
字符串名称[]=新字符串[行];
//---输入---
//从1开始的第一列城市
对于(int k=0;k1)
{
系统输出打印(“,”);
}
系统输出打印(x[i][j]);
}
系统输出打印(“)”;
System.out.println(“”);
}
//打印出距离
System.out.println(“”);
System.out.println(“”);
//行的阶乘
intz;
int num=1;
对于(z=1;z请尝试以下方法:
total[b] = (int) (Math.sqrt( Math.pow( 2, ( x[b+1][b+1] - x[b][b+1] ) ) + Math.pow( 2, ( x[b+2][b+2] - x[b][b+2] ) ) ) );
正如错误中所述,整数不如双精度值精确,您将失去精度。因此java需要显式转换
当然,这并不能解决精度损失的问题。在某些情况下,这是可以接受的。如果精度损失是可以接受的,则开发人员必须根据具体情况作出决定。如果精度不能损失,则除了将变量分配给具有相同或更好精度的其他变量外,别无其他方法在这种情况下,数组int[]total
必须声明为double[]total
double[] total = new double[num];
for (int b = 0; b < num; b++) {
total[b] = Math.sqrt( Math.pow( 2, ( x[b+1][b+1] - x[b][b+1] ) ) + Math.pow( 2, ( x[b+2][b+2] - x[b][b+2] ) ) );
}
double[]总计=新的double[num];
for(int b=0;b
您正在为填充了int类型的数组中的int指定一个double。这涉及警告,但不涉及OP首先得到警告的原因-精度仍在丢失。请您再解释一下好吗?@AndyTurner我觉得OP可以接受精度丢失。无论如何,我升级了我的答案还包括一个没有精度损失的解决方案。
double[] total = new double[num];
for (int b = 0; b < num; b++) {
total[b] = Math.sqrt( Math.pow( 2, ( x[b+1][b+1] - x[b][b+1] ) ) + Math.pow( 2, ( x[b+2][b+2] - x[b][b+2] ) ) );
}