Deep Hypnosis Deep Hypnosis - 6 months ago 10
Java Question

I am getting two errors while compiling this java code

so i am getting these two errorsenter image description here. How can I solve them? and one more thing can anyone give me short code for variable c.

double bucky[]= {7.8,5.9,4.1};
double test[]= {0,0,0};

for(int counter=0;counter<bucky.length;counter++){
double c=(12-(test[0]*bucky[0]+test[1]*bucky[1]+test[2]*bucky[2]))/bucky[counter];

int x= (int) c;
test[counter]=x;
System.out.println(test[counter]);

}

double summation=test[0]*bucky[0]+test[1]*bucky[1]+test[2]*bucky[2];

double loss=12-summation;
System.out.println("Loss is "+ loss);

int level=2;
if(test[level]>0){
for(int jass=0;jass<(1-level);jass++){
double test2[];
double test2[jass]=test[jass];
System.out.println("level after "+ test2[jass]);
}

}
else{
System.out.println("not less");
}

Answer

The syntax for declaring and using an array would be something like:

double[] test2 = new double[someLengthValue];
test2[someIndex] = someValue;

But the whole thing is kind of moot, really. In your loop you re-declare a new array every iteration and try to set a single value in that array. Then all you do is print that value. You don't need that array at all:

for(int jass=0;jass<(1-level);jass++){
    System.out.println("level after "+ test[jass]);
}