samedi 5 août 2017

Java if condition

import java.util.*;
public class Solution {

static void solve(int [] a, int [] b){
    int x=0;
    int y=0;
    for (int i=0; i<3; i++){

        if(a[i] > b[i]) x++; //this never executes even if a[i] is less than b[i]
        if(a[i] < b[i]) y++;

    }
    System.out.printf("%d %d", x, y);
    return;
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int a[] = new int [3];
    int b[] = new int [3];
    for(int x: a){
        a[x] = in.nextInt();
    }
    for(int y: b ){
        b[y] = in.nextInt();

    }
    solve(a,b);

}
}

In this program, the x never gets incremented in the solve function. It works only if the conditional statement is changed to 'if(a[i] <= b[i]) x++;'. Have I written it wrong ?

Aucun commentaire:

Enregistrer un commentaire