+ 1

what's wrong with the code?

fun main(args: Array<String>) { var age=readLine()!!.toInt()     var token=readLine()!!.toFloat() var result= when{     age>token -> "You are very poor"     age<token -> "You are very wealthy"     else -> "Average level"     } println(result) }

25th Dec 2021, 3:32 PM
время Ч
6 odpowiedzi
+ 3
Your code is ok. But there was a missing CR or new line at the end of one or more lines. I've corrected this. And show you how to print it directly w/o additionel result variable fun main(args: Array<String>) { val age=readLine()!!.toInt() val token=readLine()!!.toFloat() println ( when { age>token -> "You are very poor" age<token -> "You are very wealthy" else -> "Average level" } ) } https://code.sololearn.com/cN5em9uLHySf/?ref=app
25th Dec 2021, 3:48 PM
Coding Cat
Coding Cat - avatar
+ 2
время Ч Kotlin has that keyword 'when' that can successfully replace an if-else structure. The code seems legit. But you didn't said what is the error.
25th Dec 2021, 3:46 PM
Romeo Cojocaru
Romeo Cojocaru - avatar
+ 1
You try to compare an Int to a Float. Kotlin might not like that. What is the error?
25th Dec 2021, 3:39 PM
Romeo Cojocaru
Romeo Cojocaru - avatar
0
That is, here you need to write code through the else if?
25th Dec 2021, 3:44 PM
время Ч
0
Sorry, I'm just studying and I don't understand what my problem is
25th Dec 2021, 3:45 PM
время Ч
0
thanks a lot
25th Dec 2021, 3:53 PM
время Ч