Skip to main content
removeu 57 caracteres do conteúdo
Fonte Link
Maniero
  • 484,4mil
  • 94
  • 1,3mil
  • 2,2mil
val monthString = when (month) {
    1 -> "January"
    2 -> "February"
    3 -> "March"
    4 -> "April"
    5 -> "May"
    6 -> "June"
    7 -> "July"
    8 -> "August"
    9 -> "September"
    10 -> "October"
    11 -> "November"
    12 -> "December"
    else -> "Invalid month"
}

Na verdade mesmo em Java eu faria diferente:

val monthString = if (month < 1 || month > 12) "Invalid month" else arrayOf("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")[month - 1]

Veja funcionando no ideonefuncionando no ideone. E no Coding Groundno repl.it. Também coloquei no GitHub para referência futura.

val monthString = when (month) {
    1 -> "January"
    2 -> "February"
    3 -> "March"
    4 -> "April"
    5 -> "May"
    6 -> "June"
    7 -> "July"
    8 -> "August"
    9 -> "September"
    10 -> "October"
    11 -> "November"
    12 -> "December"
    else -> "Invalid month"
}

Na verdade mesmo em Java eu faria diferente:

val monthString = if (month < 1 || month > 12) "Invalid month" else arrayOf("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")[month - 1]

Veja funcionando no ideone. E no Coding Ground. Também coloquei no GitHub para referência futura.

val monthString = when (month) {
    1 -> "January"
    2 -> "February"
    3 -> "March"
    4 -> "April"
    5 -> "May"
    6 -> "June"
    7 -> "July"
    8 -> "August"
    9 -> "September"
    10 -> "October"
    11 -> "November"
    12 -> "December"
    else -> "Invalid month"
}

Na verdade mesmo em Java eu faria diferente:

val monthString = if (month < 1 || month > 12) "Invalid month" else arrayOf("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")[month - 1]

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.

adicionou 207 caracteres ao conteúdo
Fonte Link
Maniero
  • 484,4mil
  • 94
  • 1,3mil
  • 2,2mil
val monthString = when (month) {
    1 -> "January"
    2 -> "February"
    3 -> "March"
    4 -> "April"
    5 -> "May"
    6 -> "June"
    7 -> "July"
    8 -> "August"
    9 -> "September"
    1210 -> "October"
    11 -> "November"
    1012 -> "December"
    else -> "Invalid month"
}

Na verdade mesmo em Java eu faria diferente:

val monthString = if (month < 1 || month > 12) "Invalid month" else arrayOf("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")[month][month - 1]

Veja funcionando no ideone. E no Coding Ground. Também coloquei no GitHub para referência futura.

val monthString = when (month) {
    1 -> "January"
    2 -> "February"
    3 -> "March"
    4 -> "April"
    5 -> "May"
    6 -> "June"
    7 -> "July"
    8 -> "August"
    9 -> "September"
    12 -> "October"
    11 -> "November"
    10 -> "December"
    else -> "Invalid month"
}

Na verdade mesmo em Java eu faria diferente:

val monthString = if (month < 1 || month > 12) "Invalid month" else arrayOf("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")[month]
val monthString = when (month) {
    1 -> "January"
    2 -> "February"
    3 -> "March"
    4 -> "April"
    5 -> "May"
    6 -> "June"
    7 -> "July"
    8 -> "August"
    9 -> "September"
    10 -> "October"
    11 -> "November"
    12 -> "December"
    else -> "Invalid month"
}

Na verdade mesmo em Java eu faria diferente:

val monthString = if (month < 1 || month > 12) "Invalid month" else arrayOf("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")[month - 1]

Veja funcionando no ideone. E no Coding Ground. Também coloquei no GitHub para referência futura.

adicionou 207 caracteres ao conteúdo
Fonte Link
Maniero
  • 484,4mil
  • 94
  • 1,3mil
  • 2,2mil
Stringval monthString = when (month) {
    1 -> "January"
    2 -> "February"
    3 -> "March"
    4 -> "April"
    5 -> "May"
    6 -> "June"
    7 -> "July"
    8 -> "August"
    9 -> "September"
    12 -> "October"
    11 -> "November"
    10 -> "December"
    else -> "Invalid month"
}

Na verdade mesmo em Java eu faria diferente:

val monthString = if (month < 1 || month > 12) "Invalid month" else arrayOf("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")[month]
String monthString = when (month) {
    1 -> "January"
    2 -> "February"
    3 -> "March"
    4 -> "April"
    5 -> "May"
    6 -> "June"
    7 -> "July"
    8 -> "August"
    9 -> "September"
    12 -> "October"
    11 -> "November"
    10 -> "December"
    else -> "Invalid month"
}

Na verdade mesmo em Java eu faria diferente:

val monthString = when (month) {
    1 -> "January"
    2 -> "February"
    3 -> "March"
    4 -> "April"
    5 -> "May"
    6 -> "June"
    7 -> "July"
    8 -> "August"
    9 -> "September"
    12 -> "October"
    11 -> "November"
    10 -> "December"
    else -> "Invalid month"
}

Na verdade mesmo em Java eu faria diferente:

val monthString = if (month < 1 || month > 12) "Invalid month" else arrayOf("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")[month]
Fonte Link
Maniero
  • 484,4mil
  • 94
  • 1,3mil
  • 2,2mil
Carregando