From 490199fd8a53393059fad4bf4fe7f7b765b5d41d Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Wed, 14 Aug 2024 22:53:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test1/BasicSyntax.kt | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/test1/BasicSyntax.kt b/test1/BasicSyntax.kt index 0a18422..359e93a 100644 --- a/test1/BasicSyntax.kt +++ b/test1/BasicSyntax.kt @@ -32,8 +32,8 @@ fun main(array: Array) { println(c) // 返回最大值 - println(BasicSyntax(1.0,2.0).maxOf(1, 2)) - println(BasicSyntax(1.0,2.0).maxOf2(1, 2)) + println(BasicSyntax(1.0, 2.0).maxOf(1, 2)) + println(BasicSyntax(1.0, 2.0).maxOf2(1, 2)) println(maxOf3(1, 2)) // 循环 @@ -44,6 +44,43 @@ fun main(array: Array) { for (index in items.indices) { println(index) } + + // 有多少元素 + val readOnlyShapes = mutableListOf("triangle", "square", "circle") + println("readOnlyShapes有:${readOnlyShapes.count()}个元素") + println("triangle" in readOnlyShapes) + // 添加元素 + readOnlyShapes.add("YYY") + println(readOnlyShapes) + // 移出元素 + readOnlyShapes.remove("triangle") + println(readOnlyShapes) + + // Set相关操作 + val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry") + val mutableSetOf = mutableSetOf(readOnlyFruit) + println(readOnlyFruit) + println(mutableSetOf) + println("This set has ${readOnlyFruit.count()} items") + + + // map 操作 + val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100) + println(readOnlyJuiceMenu) + // {apple=100, kiwi=190, orange=100} + + // Mutable map with explicit type declaration + val juiceMenu: MutableMap = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100) + println(juiceMenu) + + val SUPPORTED = setOf("HTTP", "HTTPS", "FTP") + val requested = "smtp" + val isSupported = requested.uppercase() in SUPPORTED // Write your code here + println("Support for $requested: $isSupported") + + val number2word = mapOf(1 to "one", 2 to "tow") // Write your code here + val n = 2 + println("$n is spelt as '${number2word[2]}'") } fun sum(a: Int, b: Int): Int { @@ -76,4 +113,5 @@ class BasicSyntax(height: Double, length: Double) { and ends here. */ -fun maxOf3(a:Int,b:Int)=if (a>b)a else b +fun maxOf3(a: Int, b: Int) = if (a > b) a else b +