samedi 4 août 2018

If blocks not execute simultaneously in java

My code:

public class Part {
public static void main(String args[]){
double []timea={12.56,12.72,14.38,14.40,14.44,21.30,21.32,21.34,21.52,21.54,21.56,21.58,21.60,21.62,21.64,21.66};
double[]timeb={12.72,14.44,21.34,21.34,21.58};
double[]code={90.0,90.0,90.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(k2);
            System.out.println("We are equal and 90");
        }
        else if(code[k2]==18.0){
            System.out.println("We are equal and 18");
        }
        if(k2<timeb.length-1) {
            k2++;
        }
    }
    else if(timea[k1]>timeb[k2]){
        System.out.println("Timea is greater than Timeb");

    }
   k1++;
}
}
}

The objective of this program when timea < timeb write some output. When timea = = timeb at that time there is a check for code number. If code number is 90.0 then write some output or if code number is 18.0 then write some other output. But there is a twist in this program.

As you can see timeb have two same values. This timeb values and code values are depend upon each other. So, at 21.34 code value is 90.0 and at the same time stamp 21.34 the code value is 18.0. Also you can see that timea has a value that 21.34. So whenever it get 21.34 if try to execute timea[k1] = = timeb[k2]. In this time stamp it also execute other two if statement we are equal and 90 and we are equal and 18.

After that it get 21.52 which is < 21.58 so it will execute Timeb is greater than timea.

I stuck upon the timea[k1] = = timeb[k2] logic. When two timestamp(e.g 21.34) is same how could I handle this situation.

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire