Calculation of next week number (constrained by max 3) with rollover What I want my code to do is that if we're on day 3 then we want to increase the week by 1. But if week number is 3 we want to reset the week to 1. The code below works but can it be simplified?

Java code

public int calculateNextWeek(int week, int day) {
    if (day == 3) {
        if (week == 3) {
            return 1;
        } else {
            return week + 1;
        }
    }

    return week;
}


Example data and expected output:

day = 3, week = 1 =&gt; 2
day = 3, week = 3 =&gt; 1
day = 1, week = 1 =&gt; 1
day = 1, week = 3 =&gt; 3
You can use the modulo operator to wrap around your week counter:

public int calculateNextWeek(int week, int day) {
    if (day == 3) {
        return (week % 3) + 1;
    }

    return week;
}