kotlin数组

学习kotlin刚刚几天,闲来无事看到网上关于程序员招租的趣闻,所以写了一个kotlin版本的电话招租,不喜勿喷!!!

fun rentPhone(): String {
    val number = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
    val phone = arrayOf(0, 2, 2, 1, 4, 2, 3, 3, 6, 3, 3)
    var phoneNum = ""
    phone.forEach {
        phoneNum += number[it]
    }
    return phoneNum
}

fun main(args: Array) {
    println("手机号码:" + rentPhone())

    println(phoneNums("13314415516"))//加密完成的字母对应手机拨号键盘,-表示1 +表示0
    println(unSecretTexr("-FE-GI-LL-O"))
}

un phoneNums(a: String): String {
    val random = Random()

    val zero = arrayOf("-", "+")
    val one = arrayOf("A", "B", "C")
    val two = arrayOf("D", "E", "F")
    val three = arrayOf("G", "H", "I")
    val four = arrayOf("J", "K", "L")
    val five = arrayOf("M", "N", "O")
    val six = arrayOf("P", "Q", "R", "S")
    val seven = arrayOf("T", "U", "V")
    val eight = arrayOf("W", "X", "Y", "Z")

    var charts = ""

    a.forEach {
        when {
            it.toString() == "1" -> charts += zero[0]
            it.toString() == "2" -> charts += one[random.nextInt(3)]
            it.toString() == "3" -> charts += two[random.nextInt(3)]
            it.toString() == "4" -> charts += three[random.nextInt(3)]
            it.toString() == "5" -> charts += four[random.nextInt(3)]
            it.toString() == "6" -> charts += five[random.nextInt(3)]
            it.toString() == "7" -> charts += six[random.nextInt(4)]
            it.toString() == "8" -> charts += seven[random.nextInt(3)]
            it.toString() == "9" -> charts += eight[random.nextInt(3)]
            it.toString() == "0" -> charts += zero[1]
        }
    }

    return charts
}

/**
 * 解密手机号码
 */
fun unSecretTexr(phone: String): String {
   
    var phoneNum = ""

    val zero = arrayOf("-", "+")
    val one = arrayOf("A", "B", "C")
    val two = arrayOf("D", "E", "F")
    val three = arrayOf("G", "H", "I")
    val four = arrayOf("J", "K", "L")
    val five = arrayOf("M", "N", "O")
    val six = arrayOf("P", "Q", "R", "S")
    val seven = arrayOf("T", "U", "V")
    val eight = arrayOf("W", "X", "Y", "Z")

    phone.forEach {
        when {
            it.toString() == "-" -> phoneNum += 1
            it.toString() == "+" -> phoneNum += 0
            one.contains(it.toString()) -> phoneNum += 2
            two.contains(it.toString()) -> phoneNum += 3
            three.contains(it.toString()) -> phoneNum += 4
            four.contains(it.toString()) -> phoneNum += 5
            five.contains(it.toString()) -> phoneNum += 6
            six.contains(it.toString()) -> phoneNum += 7
            seven.contains(it.toString()) -> phoneNum += 8
            eight.contains(it.toString()) -> phoneNum += 9
        }
    }

    return phoneNum

}

 

你可能感兴趣的:(Android)