如何验证布尔值是真还是假



首先,我为我的英语道歉。当谈到解释想法时,与编程相关的问题,我仍然很难弄清楚什么是问题,我想要。

代码 :

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 是否属实,不知道年份是否是双性恋。但是(是的,我是新手,对编程非常陌生(我只是不记得测试布尔值......所以很遗憾,我正在向你们寻求帮助......提前谢谢。

顺便说一下,终端返回的是:

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

替换

if (isLeapYearJulian == true)

if (isLeapYearJulian(age))

我想你想要这个

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

我想这就是你在这里想做的

问题是你没有调用函数(见 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;
}

为了测试布尔值,请直接将其放入if

if (boolVariable) {
    ...
}

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

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

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

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

首先,我想告诉你,你的代码最底部有一个重音符号('(。无论如何,我唯一能看出的错误是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;
    }
}

是他们每个人都在寻找,并试图检查名为"isLeapYearGregorian"和"isLeapYearJulian"的变量的值。如果希望程序检查方法返回的值,则必须使用括号"调用"方法,如下所示:

isLeapYearJulian()

isLeapYearGregorian()

如果您修复了 if else 语句,您的代码应该可以工作。

最新更新