我正在尝试采用二维数组并通过一系列计算运行它,以便将其转换为一维数组。但是,当我运行我的程序时,我不断收到这个长错误
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Duplicate local variable raw_advisor
Syntax error on token "i", delete this token
Type mismatch: cannot convert from double to double[]
Syntax error on token "i", delete this token
Type mismatch: cannot convert from double to double[]
Duplicate local variable advisor_score
Syntax error on token "i", delete this token
Type mismatch: cannot convert from double to double[]
at Advisor_Score.main(Advisor_Score.java:16)
以下是我的代码:
import java.lang.Math;
public class Advisor_Score {
public static void main(String[] args){
double l[] = {1};
double k[] = {2,2};
double m[] = {3,3,3};
double All_users[][]={l,k,m};
double sum[]={0,0,0};
double [] raw_advisor=new double [3];
double [] advisor_scored_scaled= new double [3];
double []advisor_score= new double [3];
for (int i=0;i<All_users.length;i++){
for(int j=0;j<All_users[i].length;j++){
sum[i]+=All_users[i][j];
}
double raw_advisor[i]=(sum[i]-(3*All_users[i].length))/4;
double advisor_score_scaled[i]= 2.5*(1-Math.pow(Math.E, -.5*raw_advisor[i]));
double advisor_score[i] = (2.5 + advisor_score_scaled[i]);
System.out.println(advisor_score[i]);
}
}
}
提前致谢!对不起,我是编程新手。
答案 0 :(得分:4)
在分配for循环内的值时,无法指定类型。在Java中,变量使用特定类型定义一次。该类型以后不能在程序中更改,因此不需要指定。在定义advisor_score_scaled
时,您也有拼写错误。试试这个:
import java.lang.Math;
public class Advisor_Score {
public static void main(String[] args){
double l[] = {1};
double k[] = {2,2};
double m[] = {3,3,3};
double All_users[][]={l,k,m};
double sum[]={0,0,0};
double [] raw_advisor=new double [3];
double [] advisor_score_scaled= new double [3];
double []advisor_score= new double [3];
for (int i=0;i<All_users.length;i++){
for(int j=0;j<All_users[i].length;j++){
sum[i]+=All_users[i][j];
}
raw_advisor[i]=(sum[i]-(3*All_users[i].length))/4;
advisor_score_scaled[i]= 2.5*(1-Math.pow(Math.E, -.5*raw_advisor[i]));
advisor_score[i] = (2.5 + advisor_score_scaled[i]);
System.out.println(advisor_score[i]);
}
}
}
它为我编译并运行。当然不确定它是否能给出你想要的答案。