Where is the mistake in my code? #Kotlin | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2

Where is the mistake in my code? #Kotlin

Outputs result when any input is given. fun main(args: Array<String>) { var age = readLine()!!.toInt() var result = when{ age >=0 && age <=11->"Child" age >=12 && age <=17->"Teen" age >=18 && age <=64->"Adult" else -> "Senior" } println("result") }

13th Jun 2021, 11:31 AM
Md Imraj Hossain
Md Imraj Hossain - avatar
8 Réponses
+ 14
println(result)
13th Jun 2021, 11:33 AM
Simba
Simba - avatar
+ 10
A should be lowercase in age
13th Jun 2021, 11:50 AM
Simba
Simba - avatar
+ 1
doesn't well know kotlin (and not aware of 'when' construct statement), but if you want to print the value stored in 'result', then remove the quotes: print(result)
13th Jun 2021, 11:34 AM
visph
visph - avatar
+ 1
Thanks both of you,,. Still one case is not solved. Given an age as input, you need to output the age group according to the following categories: Child: 0 - 11 Teen: 12 - 17 Adult: 18 - 64 Senior: 65+ In case the age is negative, you need to output "Invalid age". Sample Input: 42 Sample Output: Adult
13th Jun 2021, 11:37 AM
Md Imraj Hossain
Md Imraj Hossain - avatar
+ 1
what is the task description?
13th Jun 2021, 11:39 AM
visph
visph - avatar
+ 1
Ok I am trying ..the negative part missed.
13th Jun 2021, 11:41 AM
Md Imraj Hossain
Md Imraj Hossain - avatar
+ 1
Still showing error in one part. fun main(args: Array<String>) { var age = readLine()!!.toInt() var result = when{ age >=0 && age <=11->"Child" age >=12 && age <=17->"Teen" age >=18 && age <=64->"Adult" age<0->"Invalid Age" else->"Senior" } println(result) }
13th Jun 2021, 11:48 AM
Md Imraj Hossain
Md Imraj Hossain - avatar
- 1
fun main(args: Array<String>) { var age = readLine()!!.toInt() val result = when{ age >=0 && age <=11->"Child" age >=12 && age <=17->"Teen" age >=18 && age <=64->"Adult" age > 65 ->"Senior" else->"Invalid age" } println(result) } Change var to val
28th Oct 2021, 5:47 PM
Salman B
Salman B - avatar