+ 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") }
8 odpowiedzi
+ 14
println(result)
+ 10
A should be lowercase in age
+ 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)
+ 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
+ 1
what is the task description?
+ 1
Ok I am trying ..the negative part missed.
+ 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)
}
- 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