Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Method calling kotlin

I’m sure there is already an answer for this, but I’m very new and a lot of the answers I found look overly complicated and well past my experience as a programmer.

I created a class with a method, I wish to call on that method to display after it promts the user for input. I know where to call on the method. But don’t know how to in kotlin. PLEASE HELP!

Here’s my code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

fun main() {
println("Enter degrees in Fahrenheit")
}

class ComputeMethods(){
    fun tempConversion(degreesF: Double){
        var degreesF = readln().toDouble()
        var degreesC = (degreesF - 32.0) * 5.0 / 9.0;
        var conversion = "$degreesF degrees Fahrenheit = $degreesC degrees Celsius"
        println(conversion)
    }
}

>Solution :

It’s better to read user input inside main() and pass it to the method

fun main() {
    println("Enter degrees in Fahrenheit")
    ComputeMethods().tempConversion(readln().toDouble())
}

class ComputeMethods(){
    fun tempConversion(degreesF: Double){
        var degreesC = (degreesF - 32.0) * 5.0 / 9.0;
        var conversion = "$degreesF degrees Fahrenheit = $degreesC degrees Celsius"
        println(conversion)
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading