Lesson 8 Programming in Koutlin

in #programming7 years ago

  
Lesson 8 Programming in Koutlin 

![Kotlin-logo.png]()

 Application of age calculation in years   

we will ask the user to enter the year of his birth and then calculate his age in years  

fun main (args: Array <String>) 

 { 

       print ("enter your birth year:") 

       var yearofbirth: Int = readLine () !!. ToInt ()  

       var year: Int = Calendar.getInstance (). Get (Calendar.YEAR) 

       var age: Int = year - yearofbirth 

       print ("age = $ age") 

 } 

 As we learned from previous lessons, this example is clear in most of it 

 Except the line  

var year: Int = Calendar.getInstance (). Get (Calendar.YEAR) 

 In this order we defined a year variable of Type Int  as for the other part of it

  Calendar.getInstance () .get (Calendar.YEAR) 

 We have called the history library (year) from the collection of libraries in Kotlin 

 If we want to ask for this month all we have to change the word year at the end of the line by the word month 

 Calendar.getInstance () .get (Calendar.MONTH) 

 As well as for the day, week and hours ..... 

Q: Write a program that calculates the user's age in months, weeks, and days  

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.030
BTC 64294.06
ETH 3427.66
USDT 1.00
SBD 2.59