I am trying to take a two-dimensional array and run it through a series of calculations in order to transform it into a one-dimensional array. However, when I run my program I keep getting this long error
我正在尝试采用二维数组并通过一系列计算运行它,以便将其转换为一维数组。但是,当我运行我的程序时,我不断收到这个长错误
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)
Here is what my code looks like:
这是我的代码的样子:
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]);
}
}
}
Thanks in advance! Sorry I am new to programming.
提前致谢!对不起,我是编程新手。
1 个解决方案
#1
4
You can't specify the type when assigning to the values inside the for loop. In Java, variables are defined once with a specific type. That type can't be changed later on in the program, so it does not need to be specified. You also had a typo when defining advisor_score_scaled
. Try this:
分配到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]);
}
}
}
It compiles and runs for me. Not sure of course if it gives the answer you want.
它编译并运行给我。当然不确定它是否给出了你想要的答案。
#1
4
You can't specify the type when assigning to the values inside the for loop. In Java, variables are defined once with a specific type. That type can't be changed later on in the program, so it does not need to be specified. You also had a typo when defining advisor_score_scaled
. Try this:
分配到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]);
}
}
}
It compiles and runs for me. Not sure of course if it gives the answer you want.
它编译并运行给我。当然不确定它是否给出了你想要的答案。