简体   繁体   English

不兼容的类型:在Java中使用Math.sqrt和Math.pow从Double到Int的可能松散转换

[英]Incompatible Types: Possible Loosy Conversion from Double to Int using Math.sqrt and Math.pow in Java

I am getting the following error and i do not know why :( 我收到以下错误,但我不知道为什么:(

Incompatible types: possible loosy conversion from double to int

It is showing on the 109th line of the code. 它显示在代码的第109行上。 That code uses Math.sqrt and Math.pow. 该代码使用Math.sqrt和Math.pow。

The code: 编码:

        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] ) ) );

Please spare me if it is a simple error. 如果这是一个简单的错误,请饶恕我。 I started Java just yesterday, and i am trying to get a hang of it. 我是昨天才启动Java的,所以我想尝试一下它。 I am also a fairly new member of Stack Overflow :) 我也是Stack Overflow的新成员:)

Here is my code: 这是我的代码:

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] ) ) );

    }

}

}

Try this instead: 尝试以下方法:

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] ) ) ) );

An integer is not as precise as a double value and you will lose precision, as stated in the error. 整数不如双精度值那么精确,并且您将失去精度,如错误所述。 Therefore java requires an explicit cast. 因此,java需要显式转换。

This, of course, will not solve the the loss of precision. 当然,这将无法解决精度损失问题。 There are cases where this is acceptable. 在某些情况下,这是可以接受的。 If loss of precision is acceptable is a decision that must be made on a case to case basis by the developer. 如果精度损失是可以接受的,则开发人员必须根据具体情况做出决定。 If precision must not be lost then there is no other way than assigning variables to other variables that have the same or better precision. 如果一定不能丢失精度,那么除了将变量分配给具有相同或更好精度的其他变量外,别无其他方法。 In this case the array int[] total would have to be declared as double[] total instead. 在这种情况下,必须将数组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] ) ) );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM