Programming with Kotlin lesson 5

in #programming7 years ago (edited)

  Programming with Kotlin lesson 5
![Kotlin-logo.png]() In the previous lessons we learned how to define  the variables in Kotlin language and how to assign values

to them immediately after definition or later 

 But what if we want to store the value of what the user enters into the variable
Then we use 

 readLine () !! 

 If the data type entered is a text

 Example:

 we have a text variable called       name 

 we define it as we learned 

var name:String?
To store data entered by the user with the name variable  we write  

name = readLine () !! 

 If the variable type is int, double, float, ....  It must be converted before storing it  

for example  

var age: Int?

age = readLine () !!. toInt () 

 var pi: Double?  

pi = readLine () !!. toDouble ()  

Example:   

fun main (args:Array <String>)

 {

       /*in this program we will ask user to input his name and age*/

       // then we will print the info on the screen  

       print("enter your name: ")      

        var name:String?  

         name = readLine()!!  

       print("enter your age: ")  

      var age:Int? 

      age = readLine()!!.toInt()  

      println(name)  

      print(age) 

}
In this example we first ask the user to enter his name by the command  print ("enter your name:")  

Then we stored the name in a variable called name  

Then we asked the user to enter his age by the order  

print ("enter your age:") 

And stored age with a variable named age after converting it to type Int by the command  

age = readLine () !!. toInt ()  

Then we printed the input on the screen    

Comments: 

Comments are used to explain the program by typing statements that the compiler  ignores but that will help us a lot if we come back to modify our program for a period of time  

Comments are added to the program using the // symbol if the comment is from a single line 

if the comment is multi-line, we type it between the two symbols

 / * Multi-line

 comment 

* /  


  Q: Write a program that asks the user to enter his / her data (name, age, length, weight, hair color, etc.) with comments  

Sort:  

oooh easy lesson as what i want. this is Kotlin so easy

yeah
Kotlin is so easy
i will try my best to make it so simple