2016-06-29 17 views
0

我收到以下错误,我不知道为什么:(不兼容的类型:从双可能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]))); 

    } 

} 

} 
+0

你正在为int类型填充的数组中的int分配一个double。 – Li357

回答

1

试试这个:

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]))); 
} 
+0

哇!非常感谢你:) – Shehab

+0

这个处理警告,但它不处理OP为什么首先得到警告的原因 - 精度仍然丢失。请你能解释一下吗? –

+0

@AndyTurner我的印象是OP的精度损失是可以接受的。无论如何,我更新了我的答案,还包括一个没有精确度损失的解决方案。 – nautical

相关问题