2016-06-29 9 views
0

にMath.sqrtとMath.powを使用してintに、私は次のエラーを取得していますし、それは第109に表示されている理由:(互換性のないタイプ:ダブルの可能性Loosy変換するJava

Incompatible types: possible loosy conversion from double to int 

私は知りませんコードの行このコードは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])))); 

整数をdouble値ほど正確ではなく、あなたが失うことになります誤差で述べたように、したがって、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

関連する問題