jeudi 4 novembre 2021

Why are the correct array values not getting printed?

I'm trying to print the marks of the rollNumber array using only nested if else. However, the output that I'm getting is:

111 Honors
111 First Division
333 Fail

Whereas the output should be

111 Honors
222 First Division
333 Fail
444 Second Division

Where am I going wrong? The code that I've written is stated below:

public class JavaApplication53 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int rollNumber[]={111, 222, 333, 444};
        int marks[]={81, 75, 43, 58};
        
        for(int i=0; i<rollNumber.length; i++) {
            if(marks[i]>49) {
                if(marks[i]>79) {
                    System.out.println(rollNumber[i] + " Honors");
                
                    if(marks[i]>59){
                        System.out.println(rollNumber[i] + " First Division");
                    } else {
                        System.out.println(rollNumber[i] + " Second Division");
                    }
                }
            } else {
                System.out.println(rollNumber[i]+ " Fail");
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire