double [] []无法转换为int [] []

时间:2016-11-17 23:19:42

标签: java

任何人都可以告诉我为什么我收到Java编译器错误 (double[][] cannot be converted to int[][])。它在我的计划中失败了。以下是整个事情:

public class Salary {

public double medianPay (int p, int [][] pay)
{
    double median = 0;
    int total = 0;
    int staff = pay[p].length;

    for (int i = 0; i < staff; i++ )
    {
        total = total + pay[p][i];
        median = total / staff;
    }
    return total;
}        

public int totalPay (int p, int[][] pay)
{
  int total = 0;
  int staff = pay[p].length;

  for (int i = 0; i < staff; i++ )
  {
      total = total + pay[p][i];
  }
  return total;
}   

public int totalStaff (int p, int [][] pay)
{
    int staff = pay[p].length;
    return staff;
}

public static void main ( String [] args ) {

    double salaries [][] = {    
        {49920, 50831, 39430, 54697, 41751, 36110, 41928, 48460, 39714, 49271, 51713, 38903},       //Alermit (row 0)
        {45519, 47373, 36824, 51229, 36966, 40332, 53294, 44907, 36050, 51574, 39758, 53847},      //Logway (row 1)
        {54619, 48339, 44260, 44390, 39732, 44073, 53308, 35459, 52448, 38364, 39990, 47373}     //Felter    (row 2)
    };

    System.out.println("Which company would you like salaray statistics for?: ");
    System.out.println("Press 0 - Alhermit");
    System.out.println("Press 1 - Logway");
    System.out.println("Press 2 - Felter");

    int user_input = 3;
    String CorpName = "";

    if (user_input < 3)
    {
        if (user_input == 0)
            CorpName = " Alhermit ";
        else if (user_input == 1)
            CorpName = " Logway ";
        else if (user_input == 2)
            CorpName = " Felter ";

        double median = medianPay(user_input, salaries);
        int total = totalPay(user_input,salaries);
        int staffNumber = totalStaff(user_input,salaries);


        System.out.println("The Average Salary of " + CorpName + "is -" + median);
        System.out.println("The Combined Salries of " + CorpName + "is -" + total);
        System.out.println( CorpName + " Has " + staffNumber + " Employee's");
    }
    else
        System.out.println("Please Try Again");
}   
}

我在这3行中的单词salaries收到错误:

double median = medianPay(user_input, salaries);
int total = totalPay(user_input,salaries);
int staffNumber = totalStaff(user_input,salaries);

2 个答案:

答案 0 :(得分:1)

你传递了一个double值,所以你必须要一个双变量来接受它。你不能在这里转换任何东西。您可以做什么,您可以在代码中键入强制转换为int。

 public static double medianPay (int p, double[][] salaries)
{
    double median = 0;
    int total = 0;
    int staff = salaries[p].length;

    for (int i = 0; i < staff; i++ )
    {
        total = (int) (total + salaries[p][i]);
        median = total / staff;
    }
    return total;
}

答案 1 :(得分:0)

您的salaries变量属于double [][]类型。您尝试将其传递给接受int [][]类型参数的函数,例如。 medianPay()。这是一种类型的冲突;因此你的错误。

您可能希望将函数转换为接受并使用double[][]。或者,看到您的工资都是整数,您可以将salaries转换为int[][]类型。如果采用后一种选择,请注意在划分时,您将四舍五入到最接近的整数值。