μ ν¨μ λ΄ ν¨μ μ¬μ© μ΄μ
νλΌλ―Έν°?
μ ν¨ν€μ§ μ΄μ
λͺ¨λλ³λ‘ ꡬλΆ, νμ μ μ©μ΄
μ μλ°μλ μλ μ½νλ¦°μ κΈ°λ₯(λ²μ: '..' 'unitl' ',') (μμ: 'in')
Kotlin ν¨μμ μ
import kotlin.math.PI // kotlin.math ν¨ν€μ§μμ PI μμλ₯Ό κ°μ Έμ΄
fun circleArea(radius: Double): Double { //ν¨μμ μ μ
return PI*radius*radius
}
fun main() {
print("Enter radius: ")
val radius = readLine()!!.toDouble()
println("Circle area: ${circleArea(radius)}")
}
fun circleArea(radius: Double): Double
1 2 3 4 5
1 ν¨μμ μ μκ° λ€μ λμ΄μ λͺ μ
2 ν¨μμ μ΄λ¦
3 첫λ²μ§Έ νλΌλ―Έν°
4 첫λ²μ§Έ νλΌλ―Έν°μ νμ
5 λ°νκ°μ νμ
Kotlin ν¨μμ νΉμ§ (1)
• νλΌλ―Έν°μ νμ μ νμ μ§μ ν΄μΌ νλ€.
fun circleArea(radius: Double): Double // (O)
fun circleArea(radius) : Double // (X)
• λ°νκ°μ΄ μλ κ²½μ°λ§ λ°νκ°μ νμ
μ μλ΅ν μ μλ€.
• Cμ void ν¨μμ ν΄λΉνλ©° μ΄λ Kotlinμμλ Unitμ΄λΌλ νμ
μ μλ΅μ
fun prompt(name: String) {
println("*** Hello, $name! ***")
}
fun prompt(name: String): Unit { //μ ν¨μμ λμΌ
println("*** Hello, $name! ***")
}
Kotlin ν¨μμ νΉμ§ (2)
• νλΌλ―Έν°μ κ°μ μμ μ΄ λΆκ°λ₯ νλ€.
• νλΌλ―Έν°λ val(λΆλ³) λ³μμ νΉμ±μ κ°μ§
fun foo(bar: Int) : Int {
bar += 1 // Error
return bar
}
• μν¨μλλ€μκ³Όκ°μ΄μμ ν΄μΌν¨
fun foo(bar: Int) : Int {
val baz = bar + 1 //μλ‘μ΄ λ³μ baz
return baz
}
μ νλκ° λ³Έλ¬ΈμΈ ν¨μ
• μ νλλ‘ ννμ΄ κ°λ₯ν ν¨μμ κ²½μ° returnμ μλ΅νκ³ =λ‘ ν¨μ μ μΈκ³Όλ³Έλ¬Έμμ°κ²°ν μμμ
fun circleArea(radius: Double): Double = PI * radius * radius
• μμμ λ°νκ°μ νμ μ΄ μ μΆκ° κ°λ₯ν κ²½μ° λ°ν νμ μλ΅κ°λ₯
fun circleArea(radius: Double) = PI * radius * radius
ν¨μμ νΈμΆ
• λ€μμ ν¨μλ₯Ό νΈμΆν λ
fun rectangleArea(width: Double, height: Double) = width * height
• λ€μμ μΈκ°μ§ ννκ° λͺ¨λ κ°λ₯νλ©° λμΌν μλ―Έμ΄λ€.
rectangleArea(w, h)
rectangleArea(width = w, height = h)
rectangleArea(height = h, width = w)
• μ¦ νλΌλ―Έν°μ μ΄λ¦μΌλ‘ μΈμλ₯Ό μ λ¬ν μ μλ€.
ν¨μμ μ€λ²λ‘λ©
• μ€λ²λ‘λ©: λμΌν μ΄λ¦μ ν¨μλ₯Ό νλΌλ―Έν° νμ μ λ³μ΄μ λ°λΌ μ¬λ¬ λ²μ μΌλ‘ μ μνλ νλ‘κ·Έλλ°
• fun readInt() = readLine()!!.toInt()
• fun readInt(radix: Int) = readLine()!!.toInt(radix) // λν¨μ λμ μ μ κ°λ₯
• ν¨μ νΈμΆμ μ€λ²λ‘λ© ν΄μ κ·μΉ
1 νλΌλ―Έν°μ κ°μμ νμ μ κΈ°μ€μΌλ‘ νΈμΆν μ μλ λͺ¨λ ν¨μλ₯Ό μ°Ύλλ€
2 λ ꡬ체μ μΈ ν¨μλ₯Ό μ μΈμν¨λ€. μ΄λ€ν¨μμ νλΌλ―Έν° νμ μ΄ λ€λ₯Έ ν¨μμ λΉνμ¬ ν¬κ΄μ μΌ κ²½μ° μ΄λ₯Ό μ μΈνλ€.
μ΄ κ³Όμ μ νλμ ν¨μκ° λ¨μ λκΉμ§ λ°λ³΅νλ€.
3 νλκ° λ¨μΌλ©΄ ν΄λΉ ν¨μλ₯Ό νΈμΆνλ€. λκ° μ΄μμ΄ λ¨μΌλ©΄ μ»΄νμΌ μλ¬λ₯Ό λ°μμν¨λ€.
ν¨μ μ€λ²λ‘λ©μ μ
• ν¨μλ€μ μ μ
1 fun mul(a: Int, b: Int) = a * b
2 fun mul(a: Int, b: Int, c: Int) = a * b * c
3 fun mul(s: String, n: Int) = s.repeat(n)
4 fun mul(o: Any, n: Int) = Array(n) { o }
• mul(1, 2) //1
• mul(1L, 2) //4
• mul("0", 3) //3
• mul("0" as Any, 3) //4
Any λ?
• λͺ¨λ κ°μ²΄μ μ΅μμ νμ
• κ°μ₯ ν¬κ΄μ μΈ κ°λ μ νμ
• as Any λ?
• λ¬Έμμ΄ "0"μ μμ κ°λ μΈ Any νμ μΌλ‘ μκ°νκ³ νΈμΆμ μν
ν¨μμ λν΄νΈ νλΌλ―Έν°
fun readInt(radix: Int = 10) = readLine!!.toInt(radix)
- μ ν¨μλ νλΌλ―Έν°κ° μ£Όμ΄μ§μ§ μμΌλ©΄ μλμΌλ‘ 10μ νλΌλ―Έν°λ‘ κ°λλ€.
val decimalInt = readInt()
val decimalInt2 = readInt(10) // μμ λμΌν νΈμΆ
val hexInt = readInt(16)
ν¨μμ κ°μμ±
• public ν¨μ
μ) public fun readInt(): Int = readLine()!!.toInt()
• νλ‘μ νΈμ μ΄λμλ ν΄λΉ ν¨μ νΈμΆ κ°λ₯
• κ°μμ± μ μΈμ΄ μλ ν¨μλ λν΄νΈλ‘ publicμΌλ‘ μ μΈλ¨
fun readInt():Int=readLine()!!.toInt() //μμ κ°μ μ μΈ
• internal ν¨μ
μ) internal fun readInt(): Int = readLine()!!.toInt()
• ν¨κ» μ»΄νμΌλλ λͺ¨λ μμμλ§ νΈμΆ κ°λ₯
• private ν¨μ
μ) private fun readInt(): Int = readLine()!!.toInt()
• κ°μνμΌ μμμλ§ νΈμΆκ°λ₯
ν¨μ λ΄ ν¨μ
• ν¨μ μμ ν¨μ μ μ κ°λ₯
• ν¨μλ₯Ό κ°μΈλ λΈλ‘μμλ§ νΈμΆ κ°λ₯
• ν¨μ λ΄μ λ³μμ μ κ·Ό κ°λ₯
fun main(args: Array<String>) {
fun swap(i: Int, j: Int): String {
val chars = args[0].toCharArray()
val tmp = chars[i]
chars[i] = chars[j]
chars[j] = tmp
return chars.concatToString()
}
println(swap(0, args[0].lastIndex))
}
ν¨ν€μ§
• μ½νλ¦° νλ‘κ·Έλ¨μ κ³μΈ΅ννμ¬ μ²΄κ³μ μΌλ‘ κ΄λ¦¬νλ λ°©λ²
• μ½νλ¦° μ½λλ₯Ό λ΄κ³ μλ λλ ν 리 ꡬ쑰μ μΌμΉνλ€.
package foo.bar.util
fun readInt(radix: Int) = readLine()!!.toInt(radix)
• μ readInt() ν¨μλ λλ ν 리 ꡬ쑰μ foo/bar/utilλλ ν 리μ μ μ₯λλ©° fooν¨ν€μ§λ΄μ barν¨ν€μ§λ΄μ utilν¨ν€μ§μ μνκ² λλ€.
λμΌ ν¨ν€μ§μμμ ν¨μ νΈμΆ
• λμΌ ν¨ν€μ§μ ν¨μλ import μμ΄ νΈμΆν μ μλ€.
λ€λ₯Έ ν¨ν€μ§μ ν¨μ νΈμΆ
• ν¨μμ μ΄λ¦μ ν¨ν€μ§ μ΄λ¦κΉμ§ λͺ¨λ λΆμ¬μ νΈμΆνλ€.
• νμΌ μμͺ½μ import λ¬Έμ ν΅νμ¬ νΈμΆν ν¨μλ₯Ό 미리 μ μ΄ λμΌλ©΄ ν¨μ μ΄λ¦λ§μΌλ‘ νΈμΆμ΄ κ°λ₯νλ€.
import ... as ...
• λ€λ₯Έν¨ν€μ§μμλλμΌνμ΄λ¦μν¨μλ₯ΌνΈμΆν λ
import foo.readInt as fooReadInt
import bar.readInt as barReadInt
fun main() {
val n = fooReadInt()
val m = barReadInt()
}
ν¨ν€μ§ μ 체 μν¬νΈ
• ν¨ν€μ§λ΄μ λͺ¨λ μ μΈμ μν¬νΈ ν λ
import kotlin.math.* // kotlin.math ν¨ν€μ§ μμ λͺ¨λ μ μΈ import
• μ 체 importλ ꡬ체μ importμ λΉνμ¬ μ°μ μμκ° λ¨μ΄μ§λ€.
import foo.readInt
import bar.*
fun main() {
val n=readInt() // foo.readInt()λ₯Ό νΈμΆ
} // (bar ν¨ν€μ§μ readInt()ν¨μλ μ°μ μμκ° λ°λ¦°λ€)
if문
• CμΈμ΄ / JavaμΈμ΄μ if λ¬Έκ³Ό λΉμ·
• fun max(a: Int, b: Int): Int {
if (a > b) return a
else return b
}
fun main(args: Array<String>) {
if (args.isNotEmpty()) {
val message = "Hello, ${args[0]}"
println(message)
}
else {
println()
}
}
• 쑰건문μ λ°λμ Boolean νμ μ΄μ΄μΌ ν¨
• CμΈμ΄μ κ²½μ° μ μν 쑰건문
μ ννμ μν if~elseλ¬Έ
• CμΈμ΄μ ? : μ°μ°μ Kotlinμμλ if elseλ‘ νν
fun max(a:Int,b:Int)=if(a>b) a else b
• μ ννμ μ¬λ¬ μ€ μ°μ°μ ν¬ν¨ν μλ μλ€. μ΄ κ²½μ° λ§μ§λ§ λ¬Έμ₯μ κ°μ μ΄μ©
• fun main() {
val s = readLine()!!
val i = s.indexOf("/")
val result = if (i >= 0) {
val a = s.substring(0, i).toInt()
val b = s.substring(i + 1).toInt()
(a/b).toString()
} else ""
println(result)
}
if λ¬Έμμ μκ³Ό returnλ¬Έ νΌμ©νκΈ°
• μλ νλ‘κ·Έλ¨μ μ μμ μΌλ‘ μ»΄νμΌ λλ€.
fun renamePackage(fullName: String, newName: String): String {
val i = fullName.lastIndexOf('.') // λ§μ§λ§ . μμΉλ₯Ό μ°Ύμ
val prefix = if (i >= 0) fullName.substring(0, i + 1) else return newName
return prefix + newName
}
λ²μ νμ
• λ²μ (range)λ₯Ό νννκΈ° μν νμ
val chars = 'a' .. 'h' // 'a'μμ 'h'κΉμ§μ λͺ¨λ λ¬Έμ
val two_digits = 10 .. 99 // 10λΆν° 99κΉμ§μ λͺ¨λ μ μ
val zero2One = 0.0 .. 1.0 // 0λΆν° 1κΉμ§μ λͺ¨λ λΆλμμμ μ
• in μ°μ°: νΉμ κ°μ΄ λ²μμ ν¬ν¨λλμ§λ₯Ό νλ¨νλ μ°μ°
val num = readLine()!!.toInt()
println(num in 10..99) // num>=10 && num<=99
println (num !in 10 .. 99) // !(num in 10 .. 99)
println("def" in "abc" .. "xyz") // λ¬Έμμ΄λ λΉκ΅ κ°λ₯νλ―λ‘ λ²μ κ°λ₯
until, downTo, step
• until
val twoDigits = 10 until 100 // 10 .. 99μ κ°μ. 100μ ν¬ν¨λμ§ μμ
• downTo: λ²μμ μλλ‘ λ΄λ €κ°λ λ°©ν₯μ±μ μΆκ° (μ§ν νμ )
println(5 in 10 downTo 1) // true
println(5 in 10 .. 1) // false: λΉ μ§νμ
println(5 in 1 downTo 10) // false: λΉ μ§νμ
• step: μ§νμ κ°κ²©μ μ§μ (λ°λμ μμμ¬μΌ ν¨)
• 1..10 step3 //1,4,7,10
• 15 downTo 9 step 2 //15,13,11,9
λ¬Έμμ΄, λ°°μ΄κ³Ό λ²μ
λ¬Έμμ΄.substring()μ λ²μ μ¬μ©νκΈ°
"Hello, World".substring(1 .. 4) // ello
"Hello, World".substring(1 until 4) // ell
"Hello, World".substring(1, 4) // ell: substring(1 until 4)μ κ°μ λμ
• λ°°μ΄.sliceArray()μ λ²μ μ¬μ©νκΈ°
IntArray(10) { it*it }.sliceArray(2 .. 5) // 4, 9, 16, 25
IntArray(10) { it*it }.sliceArray(2 until 5) // 4, 9, 16
in/!inμ λ°°μ΄, λ¬Έμμ΄ λ±μ μ¬μ©νκΈ°
• in/!inμ λ°°μ΄, λ¬Έμμ΄ λ±μ μμκ° μλμ§λ₯Ό νμ
ν λ μ¬μ© κ°λ₯
• val numbers = intArrayOf(3, 7, 2, 1)
• val text = "Hello!"
• println(2 in numbers) //true
• println(9 !in numbers) //true
• println(4 in numbers) //false
• println('a' in text) //false
• println('H' in text) //true
• println('h' !in text) //true
when ꡬ문
• if...elseif...elseif...μ κΉλν λμ
fun hexDigit(n: Int): Char {
if(n in 0 .. 9) return '0' + n
else if (n in 10 .. 15) return 'A' + n – 10
else return '?'
}
fun hexDigit(n: Int): Char {
when {
n in 0 .. 9 -> return '0' + n
n in 10 .. 15 -> return 'A' + n – 10
else -> return '?'
}
}
• whenμμμΌλ‘μ¬μ©κ°λ₯
fun hexDigit(n: Int) = when {
n in 0 .. 9 -> '0' + n
n in 10 .. 15 -> 'A' + n – 10
else -> '?'
}
• whenμ λμ νΉμ
fun hexDigit(n: Int) = when(n) {
in 0 .. 9 -> '0' + n
in 10 .. 15 -> 'A' + n – 10
else -> '?'
}
do-while문
fun main() {
var sum = 0
do {
val num = readLine()!!.toInt()
sum += num
} while (num != 0)
println("Sum: $sum")
}
while문
fun main() {
val num = Random.nextInt(1, 101)
var guess = 0
while (guess != num) {
guess = readLine!!.toInt()
if (guess < num)
println("Too small")
else if (guess > num)
println("Too big") }
}
println("Right: it's $num")
}
for문
fun main() {
val a=IntArray(10){it*it} //0,1,4,9,16,...
var sum = 0
for (x in a) { // xλ iterationλ§λ€ λΆλ³ λ³μλ‘ μλ (val)
sum+=x
}
println("Sum: $sum")
}
"it"μ λλ€ ννμ λ΄μμ νμ¬ μ²λ¦¬ μ€μΈ μμλ₯Ό λνλ΄λ νΉμν ν€μλ. μ΄ λ¬Έλ§₯μμ "it"μ μ μ λ°°μ΄ aμ κ°λ³ μμλ₯Ό κ°λ¦¬ν΅λλ€. λλ€ ννμ { it * it }μ λ°°μ΄ aμ μμλ₯Ό ν΄λΉ μΈλ±μ€μ μ κ³±μ κΈ°λ°νμ¬ μ΄κΈ°ννλ λ° μ¬μ©
λ°°μ΄ indexλ₯Ό μ΄μ©ν forλ¬Έ
val a = Int Array(10) { it*it }
for (i in 0 .. a.lastIndex) {
if (i % 2 == 0) {
a[i]*=2
}
}
break문
import kotlin.random.*
fun main() {
val num = Random.nextInt(1, 101)
while (true) {
val guess = readLine()!!.toInt()
if (guess < num) println("Small")
else if (guess > num) println("Big")
else break
}
println("Right: It's $num")
}
import kotlin.random.*
fun main() {
val num = Random.nextInt(1, 101)
while (true) {
val guess = readLine()!!.toInt()
val message =
if (guess < num) "Small
else if (guess > num) "Big"
else break
println(message)
}
println("Right: It's $num")
}
continue문
fun countLetters(text: String): IntArray {
val counts = IntArray('z' – 'a' + 1)
for (char in text) {
val charLower = char.toLowerCase()
if (charLower !in 'a' .. 'z') continue
counts[charLower – 'a']++
}
return counts
}
λ΄ν¬λ 루νμ λ μ΄λΈ
• breakμ continue λ¬Έμ κ°μ₯ μμͺ½μ 루νμ μ μ©
• μ΄λ₯Όλ°κΉ₯μͺ½λ£¨νμμ μ©μν€κ³ μΆμλλ μ΄λΈμ¬μ©
fun indexOf(subarray: IntArray, array: IntArray): Int {
outerLoop@ for (i in array.indices) {
for (j in subarray.indices) {
if (subarray[j] != array[i + j]) continue@outerLoop
}
return i
}
return -1
}
μμΈ λμ§κΈ°
ν¨μμ λΉμ μμ μΈ μν© λ°μμ throw λ¬Έμ ν΅νμ¬ μμΈλ₯Ό λμ§ μ μλ€.
fun parseIntNumberInBinary(s: String): Int {
var num = 0
if (s.length !in 1..31) throw NumberFormatException("Not a Number: $s")
for (c in s) {
if (c !in '0' .. '1') throw NumberFormatException("Not a Number: $s")
num=num*2+(c–'0')
}
return num
}
try-catch λ¬ΈμΌλ‘ μμΈ μ²λ¦¬νκΈ°
• μμμ λμ§ μμΈλ₯Ό try-catch λ¬ΈμΌλ‘ μ²λ¦¬ν μ μλ€.
• λμ Έμ§ μμΈκ° try λ¬ΈμΌλ‘ μ²λ¦¬λμ§ μμΌλ©΄ νλ‘κ·Έλ¨μ΄ μμΈλ‘ μΈν΄ λΉμ μ μ’ λ£λλ€.
fun readInt(default: int): Int {
try {
return readLine()!!.toInt()
} catch (e: NumberFormatException) {
return default
}
}
try-catch λ¬Έμ μμ²λΌ μ΄μ©νκΈ°
• if λ¬Έμ²λΌ try-catchλ¬Έλ μμ²λΌ μ¬μ©ν μ μλ€.
fun readInt(default: Int) = try {
readLine()!!.toInt()
} catch (e: NumberFormatException) {
default
}
finally ꡬ문μ ν΅ν΄μ μμ μ 리 νκΈ°
• finally ꡬ문μ μλ λ¬Έμ₯λ€μ μμΈ λ°μ μ¬λΆμ κ΄κ³μμ΄ λ¬΄μ‘°κ±΄ μνλλ€.
• try ꡬ문 μμμ μ¬μ©ν μμμ μ 리ν λ μ μ©νλ€.
fun readInt(default: Int) = try {
readLine()!!.toInt()
} finally {
println("μμ μ 리")
}
μ€μ΅
import java.io.BufferedReader
import java.io.FileReader
import java.io.FileNotFoundException
import java.io.IOException
fun main() {
var fd: BufferedReader? = null
try {
fd = BufferedReader(FileReader("students.txt"))
val numStudents = fd.readLine().toInt() //3
for (i in 0 until numStudents) {
try {
val name = fd.readLine() ?: ""
val point = fd.readLine().toInt()
fun printGrade(){
val grade = when {
point >= 90 -> "A"
point in 80 until 90 -> "B"
point in 70 until 80 -> "C"
point in 60 until 70 -> "D"
else -> "F"
}
println("$name has grade $grade")
}
printGrade()
}
catch (e2: IOException) {
println("File I/O Error")
}
}
} catch (e1: FileNotFoundException) {
println("File Not Found.")
}finally {
fd?.close()
}
}
'π»ββοΈμ κ³΅κ³΅λΆ > κ°μ²΄μ§ν₯νλ‘κ·Έλλ°' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
νλ‘νΌν° (0) | 2023.09.15 |
---|---|
ν΄λμ€ (0) | 2023.09.09 |
μ½νλ¦° κΈ°μ΄ (0) | 2023.09.02 |
μ½νλ¦° λ¬Έλ² (0) | 2023.08.31 |