missing variable
I wrote this method to take an int[]b
then creates an array of int[] Per
and fill it by taking each index i
in b
and uses another method to return an int
value to be as an index of the new array Per with value of b[i]
, but the small and stupid problem is it says can't find symbol i
in line Per [index-1]= b[i]
;
any idea
public static int [] intial(int [] b)
开发者_如何学JAVA {
int [] Per =new int [64];
int index;
for(int i=0;i<b.length;i++)
index=itable(i);
Per [index-1]= b[i];
return Per;
}
Change
for(int i=0;i<b.length;i++)
index=itable(i);
Per [index-1]= b[i];
to
for(int i=0;i<b.length;i++) {
index=itable(i);
Per [index-1]= b[i];
}
Not including the braces { }
in the loop make the compiler consider the first line only as the body of the loop and since i
is a loop variable it wont be visible. Including the braces changes all this and make the two statement part of the loop body.
public static int [] intial(int [] b) {
int[] Per = new int[64];
int index,b;
for (int i=0; i<b.length; i++) {
index=itable(i);
Per[index-1] = b[i];
}
return Per;
}
I don't completely understand what you're trying to achieve, however the error you are getting is because you are re-declaring a variable named b
. If you look you can see that you named the parameter to your method b
, as well as declaring an int variable named b
. To get this to compile you'll either need to rename the method parameter, or the local variable.
Additionally, if in the line Per [index-1]= b[i];
you intend to reference the local variable, you will need to change its declaration to be an array of int.
public static int [] intial(int [] b)
{
int [] Per =new int [64];
int index; // varible b was atypo
for(int i=0;i<b.length;i++){ //here was the error because i did not wrote {}
index=itable(i); //method returns int value less than 64
Per [index-1]= b[i]; }
return Per;
}
I don't understand this line :
int index, b;
you are creating a second variable named "b", hiding the variable given as an argument (int[] b).
And please use lowercase letters at the beginning your variable names. "Per" is confusing, i thought it was a class name. Use "per" instead.
Here is a better version of what you are doing :
public static int [] intial(int [] b)
{
int [] per = new int [64];
for(int i=0; i<b.length; ++)
per [itable(i)-1]= b[i];
return per;
}
EDIT : After the question is edited, I am not sure.
There are many things wrong in this.
public static int [] intial(int [] b)
{
int Per []=new int [64];
int index, b; // b is redefined
for(int i=0;i<b.length;i++)
index=itable(i); // what is itable???
Per [index-1]= b[i]; // what is index??
return Per;
}
public static int[] intial(int[] b) {
int[] Per = new int[64];
for(int i = 0; i < b.length; i++)
Per[itable(i)-1] = b[i];
return Per;
}
精彩评论