lundi 21 octobre 2019

Why does it return the wrong String?

I'm new to Java, so apologies beforehand.

I'm making a method that takes the exam mark of a student (int mark) and returns a grade (String grade). But it sometimes returns the wrong grade.

public String calculateGrade(int mark) {
        String grade = "";
        if (mark < 40) {
            return ("Fail"); 
            }
        else if (mark < 50) {
            return ("3rd");
            }
        else if (mark < 60) {
            return ("2ii");
            }
        else if (mark < 70) {
            return ("2i");
            }
        else if (mark >= 70) {
            return ("1st"); 
            }
        else if (mark < 0) {
            return ("Invalid mark");
            }
        else if (mark > 100) {
            return ("Invalid mark"); 
            }
        return grade;
        }
System.out.println(labExample.calculateGrade(50));

The problem is that whenever i put in a value -1 it returns as "Fail" instead of "Invalid code." The same goes for over 100 as it returns as "1st" instead of "Invalid code."

Even though I've stated what to return if the grade is <0 or >100 it seems to ignore it.

Any help or pointers would be great. Thanks.

Aucun commentaire:

Enregistrer un commentaire