vendredi 10 janvier 2020

Simplify if statement with functions

I am working on this calendar project for a school seminar. It is supposed to print three months that go after each other. (You specify you want it to print January and it printes December of previous year, January and February).

I have this if statement for this, but I would like to simplify it into three lines of code. How do I do it? Is it possible? Thanks a lot.

It is Java btw.

if (month == 1) {
    printMonth(12, year-1);
    printMonth(month, year);
    printMonth(month+1, year);
} else if (month == 12) {
    printMonth(month-1, year);
    printMonth(month, year);
    printMonth(1, year+1);
} else {
    printMonth(month-1, year);
    printMonth(month, year);
    printMonth(month+1, year);
}

Aucun commentaire:

Enregistrer un commentaire