简体   繁体   中英

How to verify if a boolean is true or false

First of all, I apologize about my english. When it comes to explaining ideas, problems linked to programming I still have troubles to be clear about what is wrong and I want.

The code :

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

The thing is that I would like to see if isLeapYearGregorian and isLearYearJulian are true or not to know if the year is bisextile. But (yes I'm new, very new to programming) I just can't remember to test a boolean ... So with a lot of a shame, I'm asking help to you guys ... Thanks in advance.

By the way, the terminal is returning this :

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

Replace

if (isLeapYearJulian == true)

with

if (isLeapYearJulian(age))

I think you want this

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

or more simply

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

or even more simply

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

and

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

I suppose that's what you are trying to do here

The problem was that you did not call the functions (see answer by https://stackoverflow.com/users/1361491/gjhuizing ).

Also, you could simplify your code:

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

For testing boolean put it directly in the if :

if (boolVariable) {
    ...
}

However yhe problem here is different - there should be actually function call so the parenthesis and parameters are needed:

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

isLeapYearjulian is not a variable here,
its an identifier that refers to a function name:
Correct it to:

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

First of all, I want to tell you that you have an accent mark (`) at the very bottom of your code. Anyways, the only thing I can see wrong with this is 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;
    }
}`

Anyways, the only thing I can see wrong with this is that your last two methods,

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

and

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

is that they are each looking for, and trying to check the value of VARIABLES named 'isLeapYearGregorian' and 'isLeapYearJulian.' If you want your program to check the value that your the methods return, you must 'call' your method by using parenthesis like so:

isLeapYearJulian()

and

isLeapYearGregorian()

Your code should work if you fix your if else statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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