Write a program that takes an integer as input representing a month number (1-12) and prints the corresponding season using a when expression. Months 12, 1, 2 are winter; 3, 4, 5 are spring; 6, 7, 8 are summer; and 9, 10, 11 are fall.
When expression
Mapping month numbers to seasons
Report a typo
Sample Input 1:
3Sample Output 1:
SpringSample Input 2:
7Sample Output 2:
SummerWrite a program in Kotlin
import java.util.Scanner
fun main() {
val scanner = Scanner(System.`in`)
// Read the input month number
val monthNumber = scanner.nextInt()
// Use a when expression to determine the corresponding season
val season = when (monthNumber) {
// TODO: Add cases for each month range and the corresponding season
else -> "Invalid month number"
}
println(season)
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.