我收到以下錯誤,我不知道爲什麼:(不兼容的類型:從雙可能Loosy轉換爲INT使用Math.sqrt和Math.pow在Java中
Incompatible types: possible loosy conversion from double to int
它顯示在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,我試圖爲了得到它的一個竅門,我也是堆棧溢出的一個相當新的成員:)
這裏是我的代碼:
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])));
}
}
}
你正在爲int類型填充的數組中的int分配一個double。 – Li357