Learning Algorithm with Kotlin
- There are two ways to input the value
fun main(){
var mainValue = readLine()!!.toInt()
println(mainValue);
var detailValue = readLine()!!.toString()
println(detailValue);
}
- This is also one of the framework of
Java
, but it's possible to use while you code onKotlin
import java.util.Scanner;
fun main(){
var sc: Scanner = Scanner(System.`in`);
// Check new input exist
while(sc.hasNextLine()){
var getInput = sc.nextInt();
// # 1. If there is no input line
if(!(sc.hasNextLine())){
break;
}
// #2. Or you can deal with when user inputs
if(getInput == 0){
break;
}
}
}
- Without using
hasNextLine()
orhasNext()
, you can input directly fromuserInput
import java.util.Scanner;
fun main(){
var sc: Scanner = Scannr(System.`in`);
var a = sc.nextInt();
println(a);
}
- If you want to get values more than
2
byuserInput
, you could use like this
import java.util.Scanner;
fun main(){
var sc: Scanner = Scanner(System.`in`);
var a = sc.nextInt();
var b = sc.nextInt();
// # 1. First way to `print` the values
println(a);
println(b);
// # 2. Second way to `print` the values
println("$a");
println("$b");
// # 3. Third way to `print` the values
println("${a} ${b}");
}