gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:02:29 25 4
gpt4 key购买 nike

谁能告诉我为什么我收到 Java 编译器错误(double[][] 无法转换为 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);

最佳答案

您正在传递一个 double 值,因此您必须需要一个 double 变量来接受该值。你不能在这里转换任何东西。你可以做什么,你可以在代码中输入cast to 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;
}

关于java - double[][] 无法转换为 int[][],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40666799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com