2016-06-29 56 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

相關問題