简体   繁体   中英

Why isn't the rest of my if-else branch executing?

So I posted this program last night, and still being the OOP padawan that I am, I'm confused as to why the rest of my if-else in my method won't execute. I ran the de-bugger and it only ever looks at my if and never the if-else 's, before executing my return statement. This results in my output not changing as it is supposed to do. Here is the mentioned method:

public String getBadge(int requestedStat) {
        String badgeOutput = "";
        if (requestedStat >= 0 && requestedStat <=9) {
            for (int i = 0; i < badgeRanks.length-5; i++) {
                if (gamerActions[requestedStat] >= badgePoints[requestedStat][i]
                        && gamerActions[requestedStat] < badgePoints[requestedStat][i + 1]) {
                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[i];
                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][i + 1]
                        && gamerActions[requestedStat] < badgePoints[requestedStat][i + 2]) {
                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[i + 1];
                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][i + 2]
                        && gamerActions[requestedStat] < badgePoints[requestedStat][i + 3]) {
                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[i+2];
                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][i + 3]
                        && gamerActions[requestedStat] < badgePoints[requestedStat][i + 4]) {
                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[i+3];
                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][i + 4]
                        && gamerActions[requestedStat] < badgePoints[requestedStat][i + 5]) {
                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[i+4];
                } else {
                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[i+5];
                }
            } 
        } else {
            badgeOutput = "No Badges Available";
        }
        return badgeOutput;
    }

I know my design probably isn't the most slick way to do it, that's what I got. What am I doing wrong? Also here is the other post with the entirety of the other classes: Difficulty navigating 2d String array that correlates to another 2d int array

An if statement, an else if stattement, and an else statement should cover different, non-overlapping conditions that are possible in your code.

For example:

class IfElseDemo {
    public static void main(String[] args) {

        int testscore = 76;
        char grade;

        if (testscore >= 90) {
            grade = 'A';
        } else if (testscore >= 80) {
            grade = 'B';
        } else if (testscore >= 70) {
            grade = 'C';
        } else if (testscore >= 60) {
            grade = 'D';
        } else {
            grade = 'F';
        }
        System.out.println("Grade = " + grade);
    }
}

It's hard to tell without your full program, but it seems like your else statements overlap your if statement. Since the if statement is satisfied, the program does not need to consider the else if's.

From https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

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