Standard input Standard input is a stream of data that goes into the program. By default, standard input gets data from the keyboard but it is possible to get it from a file. Kotlin functions Kotlin has a useful function to read data from the standard input. It is supported by the operating system. It is readline . It reads the whole line as a string. fun main () { val line = readLine ()!! println (line) } If you need to get a number from the input, you can use this construction: fun main () { val line = readLine ()?. toInt ()!! println (line) } To accept multiple words in a single use the following code val (a , b) = readLine ()!!. split ( ' ' ) println ( " $ a , $ b " ) val (c , d , e) = readLine ()!!. split ( ' ' ) println ( " $ c , $ d , $ e " ) Java Scanner Another way to obtain data from the standard input is to use the Java Scanner. Scanner allows a program to read values of different types (strings, numbers, etc) from ...
What is a variable? A variable is a storage for a value, which can be a string, a number, or something else. Every variable has a name (or an identifier) to distinguish it from other variables. You can access a value by the name of the variable. Declaring variables Kotlin provides two keywords val (for value) declares an immutable variable (just a named value or a constant ), which cannot be changed after is has been initialized. var (for variable) declares a mutable variable , which can be changed (as many times as needed) Both val and var keywords provide you a variable! To assign a certain value to a variable, we should use the assignment operator = It is also possible to declare and initialize a variable with the value of another variable. Storing different types of values There is one restriction for mutable variables (the ones declared with the keyword var), though. When reassigning their values, you can only use new values of the same type as the initial one. For implemen...
Comments
Post a Comment