如何验证布尔值是true还是false

时间:2016-10-15 20:42:23

标签: java

首先,我为我的英语道歉。当谈到想法,与编程有关的问题时,我仍然有麻烦要明确错误和我想要的。

代码:

public static boolean isLeapYearJulian(int year) {
    // Modifier le code ci-dessous
        if (year % 4 == 0) {
            return true;
        } 
        else {
            return false;
        }

    }

    public static boolean isLeapYearGregorian(int year) {
    // Modifier le code ci-dessous
        if ((year % 4 == 0) && (year % 100 != 0) || (year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0)) {
            return true;
        }
        else {
            return false;
        }

    }

    // EXERCICE 2 QUESTION 2
    public static int daysInYearJulian(int year) {
    // Modifier le code ci-dessous
        if (isLeapYearJulian == true) {
            return 366;
        }
        else {
            return 365;
        }
    }

    public static int daysInYearGregorian(int year) {
    // Modifier le code ci-dessous
        if (isLeapYearGregorian == true) {
            return 366;
        }
        else {
            return 365;
        }
    }`

问题是,我想看看isLeapYearGregorian和isLearYearJulian是否真实,不知道这一年是否是两性的。但是(是的我是新手,非常新的编程)我只是记不起来测试一个布尔...所以有很多耻辱,我要求帮助你们......提前谢谢。< / p>

顺便说一句,终端正在返回:

Calendar.java:47: error: cannot find symbol
        if (isLeapYearJulian == true) {
            ^
  symbol:   variable isLeapYearJulian
  location: class Calendar
Calendar.java:57: error: cannot find symbol
        if (isLeapYearGregorian == true) {
            ^
  symbol:   variable isLeapYearGregorian
  location: class Calendar
2 errors

7 个答案:

答案 0 :(得分:2)

替换

if (isLeapYearJulian == true)

if (isLeapYearJulian(age))

答案 1 :(得分:2)

我想你想要这个

public static int daysInYearGregorian(int year) {
    boolean isLeapYearGregorian = isLeapYearGregorian(year);
    if (isLeapYearGregorian) {
        return 366;
    }
    else {
        return 365;
    }
}`

或更简单

public static int daysInYearGregorian(int year) {
    if (isLeapYearGregorian(year)) {
        return 366;
    }
    else {
        return 365;
    }
}`

甚至更简单

public static int daysInYearGregorian(int year) {
    return isLeapYearGregorian(year) ? 366 : 365;
}`

答案 2 :(得分:1)

// EXERCICE 2 QUESTION 2
public static int daysInYearJulian(int year) {
// Modifier le code ci-dessous
    if (isLeapYearJulian(year)) {
        return 366;
    }
    else {
        return 365;
    }
}

public static int daysInYearGregorian(int year) {
// Modifier le code ci-dessous
    if (isLeapYearGregorian(year)) {
        return 366;
    }
    else {
        return 365;
    }
}

我想这就是你要在这里做的事情

答案 3 :(得分:1)

问题在于你没有调用这些函数(参见https://stackoverflow.com/users/1361491/gjhuizing的答案)。

此外,您可以简化代码:

public static boolean isLeapYearJulian(int year) {
  return (year % 4 == 0);
}

public static boolean isLeapYearGregorian(int year) {
  if (year % 400 == 0) return true;
  if (year % 100 == 0) return false;
  return (year % 4 == 0);
}

// EXERCICE 2 QUESTION 2                              
public static int daysInYearJulian(int year) {
  return isLeapYearJulian(year) ? 366 : 365;
}

public static int daysInYearGregorian(int year) {
  return isLeapYearGregorian(year) ? 366 : 365;
}

答案 4 :(得分:0)

对于测试布尔值,将其直接放在if

if (boolVariable) {
    ...
}

然而,这里的问题是不同的 - 实际上应该有函数调用,所以需要括号和参数:

if (isLeapYearJulian(year)) {
    ...
}

答案 5 :(得分:0)

isLeapYearjulian在这里不是变量,
它是一个引用函数名称的标识符:
纠正它:

 public static int daysInYearJulian(int year) {
        if (isLeapYearJulian(year) == true) {
            return 366;
        }
        else {
            return 365;
        }
    }

答案 6 :(得分:0)

首先,我想告诉你在代码的最底部有一个重音符号(`)。无论如何,我唯一能看到错误的是t

 // EXERCICE 2 QUESTION 2
public static int daysInYearJulian(int year) {
// Modifier le code ci-dessous
    if (isLeapYearJulian == true) {
        return 366;
    }
    else {
        return 365;
    }
}

public static int daysInYearGregorian(int year) {
// Modifier le code ci-dessous
    if (isLeapYearGregorian == true) {
        return 366;
    }
    else {
        return 365;
    }
}`

无论如何,我唯一能看到错误的是你的最后两种方法,

public static int daysInYearJulian(int year) {
// Modifier le code ci-dessous
    if (isLeapYearJulian == true) {
    return 366;
    }
    else {
        return 365;
    }
}

public static int daysInYearGregorian(int year) {
// Modifier le code ci-dessous
    if (isLeapYearGregorian == true) {
        return 366;
    }
    else {
        return 365;
    }
}

是他们每个人都在寻找,并试图检查名为&#39; isLeapYearGregorian&#39;的VARIABLES的值。并且&#39; isLeapYearJulian。&#39;如果您希望程序检查方法返回的值,则必须调用&#39;你的方法使用括号如下:

isLeapYearJulian()

isLeapYearGregorian()

如果你修改了if else语句,你的代码应该有用。