简体   繁体   English

返回布尔值的方法

[英]Methods that return boolean values

Okay, so my question is regarding boolean returns. 好的,所以我的问题是关于布尔值的回报。 For my Comp Sci homework, I have to make a course registration program using methods, and one of them is an add course method. 对于我的Comp Sci作业,我必须使用方法制作课程注册程序,其中之一是添加课程方法。 Basically, you search for the class in a catalog, and if it matches you add it to the students schedule and return a boolean value of true. 基本上,您在目录中搜索该班级,如果匹配,则将其添加到学生时间表中,并返回布尔值true。 I did this, but for some reason it is giving me an error. 我这样做了,但是由于某种原因,它给了我一个错误。 Here is the code: 这是代码:

public static boolean addCourse(
    Course[] catalog,
    Course[] mySchedule,
    int myNumCourses,
    int dept,
    int courseNum)
{
    int j;
    int i;
    int k;
    int deptCat;
    int courseNumCat;
    Course courseAdd = null;
    char checkDay;
    int checkTime;



    if (mySchedule.length == myNumCourses) {
        return false;
    }
        for (i = 0 ; i < catalog.length ; i++) {
            Course course = catalog[i];
            deptCat = course.getDepartment();
            courseNumCat = course.getCourseNumber();
            if (deptCat == dept && courseNumCat == courseNum) {
                courseAdd = catalog[i];
                break;
            }
            else continue; }
        for (j = 0 ; j < myNumCourses ; j++) {
            if (mySchedule[j] == null) {
                mySchedule[j] = courseAdd;
                return true;
                }
                else continue;
                }

    for (k = 0 ; k < mySchedule.length ; k++) {
        Course course = mySchedule[k];
        if (course != null) {
            checkDay = course.getDay();
            checkTime = course.getPeriod();
            if (checkDay == courseAdd.getDay() && checkTime == courseAdd.getPeriod()) {
                return false;
            }
        }
        else continue;

    }







}

Why doesn't it recognize the boolean return values? 为什么不能识别布尔返回值? Is it because I placed them inside a loop? 是因为我将它们放在一个循环中吗?

You need to place a return -statement at the end of your method, even if you might know it will never be reached (the compiler is not smart enough to know that, which explains the error). 需要将一个return语句来在你的方法结束时,即使你可能知道它永远不会达到(编译器是不够聪明到知道,这解释了错误)。

For instance, even this will not compile: 例如,即使这样也不会编译:

public static boolean foo() {
    if (true)
        return true;
}

unless we add a final return statement. 除非我们添加最终的return语句。 What you have is analogous. 您拥有的是类似的。

There is nothing wrong with putting your return values in loops, however, the compiler sees no guarantee that this method will return a value and thus raises an error. 将返回值放入循环中没有任何问题,但是,编译器无法保证此方法将返回值并因此引发错误。 At the very end of the method you need to return either true or false, whichever is most appropriate. 在方法的最后,您需要返回true或false,以最合适的为准。 All of your returns are within conditionals and therefor could fail to execute leaving your function with no return statement. 您的所有返回都在条件内,因此可能无法执行而没有return语句的函数。

You must explicitly return a boolean(true/false) in ALL code path.Because your function's return type is "boolean". 您必须在ALL代码路径中显式返回boolean(true / false)。因为函数的返回类型为“ boolean”。

In your case,you must add a return statement after the last loop. 对于您的情况,您必须在最后一个循环之后添加return语句。

If you don't want to write to many "return xx" statement,you can change the return type of this function to "void".And throw Exception in the false cases. 如果您不想写很多“ return xx”语句,则可以将此函数的返回类型更改为“ void”。在错误情况下抛出Exception。

I think there is a problem with the last loop. 我认为最后一个循环有问题。 If the condition for returning false is never met, it continues until it get to the end of the schedule, without returning anything. 如果从不满足返回false的条件,则它将继续进行直到到达计划的末尾,而不返回任何内容。 If you were to add a return at the end of the method this loop could fall through to it. 如果要在方法末尾添加返回值,则此循环可能会陷入失败。 Did you mean to return true after the loop, if no 'return false' is executed? 如果不是执行“ return false”,您是要在循环后返回true吗?

    for (k = 0; k < mySchedule.length; k++) {
      Course course = mySchedule[k];
      if (course != null) {
        checkDay = course.getDay();
        checkTime = course.getPeriod();
        if (checkDay == courseAdd.getDay()
            && checkTime == courseAdd.getPeriod()) {
          return false;
        }
      } else
        continue;
    }

无论您在何处使用if语句,可能的else必须返回,否则流程必须转到另一个return。ELSE缺少return。

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

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