lundi 2 septembre 2019

How to fix the "if" always going "true" [duplicate]

This question already has an answer here:

I have written a script for the left rotation in java and I have used 2 for loops in the code.

I have used if the condition for checking the first iteration in the 2nd loop and then some processing in it.

static int[] rotLeft(int[] a, int d) {
    int temp1 = 0;
    int temp = 0;
    int count = 0;
    int j;
    for(int i = 0; i < d; i++){
        for(j = a.length - 1; j > 0; j--){
                if(j == a.length - 1){
                temp1 = a[0];
                temp = a[j];
                a[j] = temp1;
            }
            else
                temp1 = a[j];
                a[j] = temp;
        }                
        }
            return a;
}

But the problem it goes true for each iteration. I know I am doing some stupid mistake.

Aucun commentaire:

Enregistrer un commentaire