简体   繁体   中英

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.

The problem is that whenever i put in a value -1 it returns as "Fail" instead of "Invalid code.""*

-1 is < 40 , so your first if 's condition is true. That means your return ("Invalid mark"); code runs, which terminates the method — none of the rest of the code is executed. return doesn't just set a return value but continue with subsequent statements, it ends the method call.

Also note that you have a logical error later, you have a series of else if statements leading up to else if (mark < 70) followed by if (mark >= 70) . That >= 70 will always be true. If mark were < 70 , the previous if condition would have been true and you would never have got there.

The correct way to do this is to check in order , probably (in this case) from lowest to highest, but perhaps with the "Invalid mark" check up front.

Other notes:

  • You don't need () around return values, return isn't a function/method.
  • You don't need else if you're doing return in the if bodies.
  • You never used grade so there's no reason to have it in the code

So:

public String calculateGrade(int mark) {
    if (mark < 0 || mark > 100) {
        return "Invalid mark";
    }
    if (mark < 40) {
        return "Fail"; 
    }
    if (mark < 50) {
        return "3rd";
    }
    if (mark < 60) {
        return "2ii";
    }
    if (mark < 70) {
        return "2i";
    }
    return "1st"; 
}

Or assigning to grade and returning it at the end instead, which some consider best practice (and others consider unnecessary complication):

public String calculateGrade(int mark) {
    String grade; // ** Note I don't do = ""; I want the compiler to warn me if I try to use it without assigning to it

    if (mark < 0 || mark > 100) {
        grade = "Invalid mark";
    }
    else if (mark < 40) {
        grade = "Fail"; 
    }
    else if (mark < 50) {
        grade = "3rd";
    }
    else if (mark < 60) {
        grade = "2ii";
    }
    else if (mark < 70) {
        grade = "2i";
    }
    else {
        grade = "1st"; 
    }
    return grade;
}

Note I needed the else s there, because I'm not return ing in the if bodies.

Your logic order is wrong. Correct logic is

public String calculateGrade(int mark) {
    String grade = "";
     if (mark < 0) {
        return ("Invalid mark");
        }
    else if (mark > 100) {
        return ("Invalid mark"); 
        }
    else 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"); 
        }
    return grade;
    }

-1 < 100 is true: so it returns "Fail"

101 >= 70 is true: so it returns "1st"

if doing it with IfEse: Check your borders first. Then do the grades.

You need to sort also the order of the various IF. In other words it matches the first valid IF.

Another possible option, to avoid sorting the IFs, could be to check if like

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

Your problem is probably the way you think of this problem.

You should first think about all the areas you need and than put them in the condition range.

public String calculateGrade(int mark) {
        String grade = "";
if(mark < 0 || mark > 100){
            grade = "fail";
        } else {
            if (mark < 40) {
                grade = ("Fail"); 
            }else if (mark < 50) {
                grade = ("3rd");

            }else if (mark < 60) {
                grade = ("2ii");

            }else if (mark < 70) {
                grade = ("2i");

            }else if = (mark >= 70) {
                grade ("1st"); 
            }
        }
        return grade;


Always check else if in ascending order.

public String calculateGrade(int mark) {
        String grade = "";
        if (mark < 0) {
            return ("Invalid mark");
            }
        else 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 > 100) {
            return ("Invalid mark"); 
            }
        return grade;
    }

Now it should work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM