包含Kotlin最基本語法的使用
1.不可變變量
val name = "stephen"
2.可變變量
var sex = "male"
3.聲明int
var bigInt :Int = Int.MAX_VALUE
var smallInt:Int = Int.MIN_VALUE
4.字符串鏈接的兩種方式 推薦第二種~
println("Biggest Int : "+ bigInt)
println("smallInt Int : $smallInt")
5.判斷字符串類型
if (sex is String){
println("true")
}else{
println("false")
}
6.$表達(dá)式簡(jiǎn)單的高級(jí)用法
var letterGrade: Char = 'A'
println("A is a Char : ${letterGrade is Char}")
//kotlin中 $表達(dá)式可以做邏輯的運(yùn)算和處理丽焊,letter is Char 是一個(gè)判斷語句,并且A是一個(gè)是Char類型丰刊,所以返回true峡继。控制臺(tái)輸出:A is a Char : true
7.類型轉(zhuǎn)換
println("3.14 to Int : " + (3.14.toInt()))
println("A to Int : " + ('A'.toInt()))
println("65 to Char : " + (65.toChar()))
//控制臺(tái)輸出
3.14 to Int : 3
A to Int : 65
65 to Char : A
8.String的簡(jiǎn)單使用
//長String
val longStr = """This is a long String"""
var fName: String = "Luke"
var lName: String = "W"
fName = "Sally"
var fullName = "$fName $lName"
println("Name : $fullName")//Run:Sally W
//int的加減邏輯也可以放在這里
println("1 + 2 = ${1 + 2}")//Run:1 + 2 = 3
//也可以直接獲取長度
println("String Length = ${longStr.length}")//Run:String Length = 21
var str1 = "A random String"
var str2 = "a random String"
//判斷是否相等
println("Strings Equal : ${str1.equals(str2)}")
//比較
println("Compare A to B : ${"A".compareTo("B")}")
//根據(jù)下標(biāo)喜颁,拿到字符。
println("2nd Index : ${str1.get(2)}") // or println("2nd Index : ${str1[2]}")
//截取
println("Index 2 - 7 : ${str1.subSequence(2, 8)}")
//判斷是否包含
println("Contains random : ${str1.contains("random")} ")
9.數(shù)組常用方法
//定義泛型數(shù)組
var myArray = arrayOf(1, 1.23, "luke")
//根據(jù)下標(biāo)拿值
println(myArray[2])
//直接改變下標(biāo)
myArray[1] = 3.14
//拿到長度
println("Array Size : ${myArray.size}")
//不用循環(huán)曹阔,直接可以通過函數(shù)判斷是否存在某一個(gè)值
println("Luke in Array : ${myArray.contains("luke")}")
//復(fù)制數(shù)據(jù)前條到新的Array
var partArray = myArray.copyOfRange(0, 1)
//拿到第一個(gè)
println("First : ${myArray.first()}")
//根據(jù)值獲取下標(biāo)
println("Luke Index : ${myArray.indexOf("luke")}")
//通過函數(shù)設(shè)置數(shù)組值 Array(size,{ x -> x * x })
var sqArray = Array(5, { x -> x * x })
println(sqArray[3])
println(sqArray.size)
//設(shè)置Array固定類型
var arrayInt: Array<Int> = arrayOf(1, 2, 3)
10.通過函數(shù)給數(shù)組賦值和遍歷
//一到十(表達(dá)式)
val oneTo10 = 1..10;
//A到Z(表達(dá)式)
val alpha = "A".."Z"
//判斷R是否存在alpha中(代碼)
println("R in Alpha : ${"R" in alpha}")
//10到1 倒序(代碼)
val tenTo1 = 10.downTo(1)
//正序(代碼)
val twoTo20 = 2.rangeTo(20)
//step可以進(jìn)行任意數(shù)量的迭代(其實(shí)在這里就是加上下標(biāo)為0的 每次加3)
val rng3 = oneTo10.step(3)
//遍歷
for (x in rng3) println("rng3 : $x")
//reversed倒序
for (x in tenTo1.reversed()) println("tenTo1 : $x")
11.判斷
//判斷語句(if和java一樣 就不寫了~)
var age = 100
//when類似java的switch 不過寫法更簡(jiǎn)單 更靈活
when (age) {
0, 1, 2, 3, 4, 5 -> println("幼稚園")
in 6..12 -> {
val grade = age - 5
println("小學(xué)${grade}年級(jí)")
}
in 13..15 -> {
val grade = age - 12
println("初$grade")
}
in 16..18 -> {
val grade = age - 15
println("高$grade")
}
else -> println("大學(xué)")
}
12.循環(huán)~
val rand = Random();
val magicNum = rand.nextInt(50) - 1
var guess = 0
while (magicNum != guess) {
guess += 1
}
println("Magic Number was $guess")
for (x in 1..100) {
if (x % 2 == 0) continue
println("Odd : $x")
if (x == 31) break
}
var arr3: Array<Int> = arrayOf(3, 6, 9)
for (i in arr3.indices) println("Muit 3 : ${arr3[i]}")
for ((index,value) in arr3.withIndex()){
println("Index : $index Value : $value")
}