The method should return an int type

public int computeStyle(String season) {
    if(season.equals("summer")){
        if (this.style.equals("toque")){
            return 8;
        }
        if (this.style.equals("sun visor")){
            return 1;
        }
        if (this.style.equals("fedora")){
            return 6;
        }
    }
    else if(season.equals("winter")){
        if (this.style.equals("toque")){
            return 1;
        }
        if (this.style.equals("sun visor")){
            return 8;
        }
        if (this.style.equals("fedora")){
            return 7;
        }
    }
    else return 5;
}

Why do I keep getting the error returned by the int method. What is wrong with this feature? Should it return int in all possible scenarios?

+4
source share
2 answers

There are two ways that are not covered:

public int computeStyle(String season) {
    if(season.equals("summer")){
        if (this.style.equals("toque")){
            return 8;
        }
        if (this.style.equals("sun visor")){
            return 1;
        }
        if (this.style.equals("fedora")){
            return 6;
        }
        //here
    }
    else if(season.equals("winter")){
        if (this.style.equals("toque")){
            return 1;
        }
        if (this.style.equals("sun visor")){
            return 8;
        }
        if (this.style.equals("fedora")){
            return 7;
        }
        //here
    }
    else return 5;
}

Solution: declare a variable with the defaut return value and assign the value correctly:

public int computeStyle(String season) {
    int result = 5;
    if(season.equals("summer")){
        if (this.style.equals("toque")){
            result = 8;
        }
        if (this.style.equals("sun visor")){
            result = 1;
        }
        if (this.style.equals("fedora")){
            result = 6;
        }
    }
    else if(season.equals("winter")){
        if (this.style.equals("toque")){
            result = 1;
        }
        if (this.style.equals("sun visor")){
            result = 8;
        }
        if (this.style.equals("fedora")){
            result = 7;
        }
    }
    return result;
}
+6
source

If your return type is int, any way your method should return int.

In this case, inside your external if elseyou still have blocks if, it means that inside the external if else, if non, if the condition is met, then it returns nothing.

In this case, you should always add a return statement at the end.

:

public int computeStyle(String season) {
    if(season.equals("summer")){
        if (this.style.equals("toque")){
            return 8;
        }
        if (this.style.equals("sun visor")){
            return 1;
        }
        if (this.style.equals("fedora")){
            return 6;
        }
    }
    else if(season.equals("winter")){
        if (this.style.equals("toque")){
            return 1;
        }
        if (this.style.equals("sun visor")){
            return 8;
        }
        if (this.style.equals("fedora")){
            return 7;
        }
    }
    else return 5;

   // If everything fails, then it ll return 0 at least
   return 0;
}
0

All Articles