This question already has an answer here:
My code:
public class Part {
public static void main(String args[]){
double []timea={1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0};
double[]timeb={3.0,5.0,6.0,8.0};
double[]code={90.0,14.0,18.0,90.0};
int k1=0,k2=0;
while(k1<timea.length){
if(timea[k1]<timeb[k2]){
System.out.println("Timeb is greater than timea");
}
else if(timea[k1]==timeb[k2]){
System.out.println("Timea is equal to Timeb");
if(code[k2]==90.0){
System.out.println("We are equal and 90");
}
else if(code[k2]==18.0){
System.out.println("We are equal and 18");
}
k2++;
}
else if(timea[k1]>timeb[k2]){
System.out.println("Timea is greater than Timeb");
}
k1++;
}
}
}
Output:
Timeb is greater than timea
Timeb is greater than timea
Timea is equal to Timeb
We are equal and 90
Timeb is greater than timea
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 4
Timea is equal to Timeb
Timea is equal to Timeb
at Part.main(Part.java:8)
We are equal and 18
Timeb is greater than timea
Timea is equal to Timeb
We are equal and 90
The output is not that I want. There is three array. Timea and Timeb the main array which used for compare. If the value of Timea < Timeb then it give some output. If timea = = timeb it then checking for the value of code array. If it get 90.0 and 18.0 it show some output. Then next else if statement is for if timea>timeb then it give some other value. But when finally timea>timeb this situation occur it terminate with out right anything.
Although it throw some error `Array Index out of bound"
Desired Output:
Timeb is greater than timea
Timeb is greater than timea
Timea is equal to Timeb
We are equal and 90
Timeb is greater than timea
Timea is equal to Timeb
Timea is equal to Timeb
We are equal and 18
Timeb is greater than timea
Timea is equal to Timeb
We are equal and 90
`Timea is greater than Timeb`
`Timea is greater than Timeb`
How to fix this problem?
Aucun commentaire:
Enregistrer un commentaire