简体   繁体   English

该方法必须返回int类型

[英]The method must return a type int

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 that the method must return type int. 为什么我一直得到方法必须返回int类型的错误。 What is wrong with this function? 这个功能有什么问题? It should return an int in every possible scenario right? 它应该在每个可能的场景中返回一个int吗?

There are two paths 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 properly: 解决方案:声明具有defaut返回值的变量并正确分配值:

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;
}

If your return type is int, any how your method must return an int. 如果返回类型为int,则表示方法必须返回int的方式。

In this case inside your outer if else , you still have if blocks, means if, inside the outer if else , if non if the condition is satisfied, then it ll return nothing. 在这种情况下,在你的外部if else ,你仍然有if块,意味着如果,在外部if else ,如果不满足条件,那么它将不返回任何内容。

In this case, you should always add a return statement at the end. 在这种情况下,您应该始终在末尾添加一个return语句。

Like this : 像这样 :

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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