-1

How can I read an integer value from the user to use in the range of a for loop?

fun main() { var n = readLine() for (i in 1..n) { var (a, b) = readLine()!!.split(' ') println(a.toInt() + b.toInt()) } } 
1

1 Answer 1

0

You can use the String.toInt() function to achieve this (the same thing that you doing in later lines).

Like this:

fun main() { val n = readLine()!!.toInt() for (i in 1..n) { val (a, b) = readLine()!!.split(' ') println(a.toInt() + b.toInt()) } } 

Also, you can replace your vars with vals since they are not changed anywhere.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.