Skip to content

Instantly share code, notes, and snippets.

@Kernelzero
Created September 10, 2024 10:27
Show Gist options
  • Save Kernelzero/2a360719db69c77c364d631f4e11ecc0 to your computer and use it in GitHub Desktop.
Save Kernelzero/2a360719db69c77c364d631f4e11ecc0 to your computer and use it in GitHub Desktop.
// 1. 숫자를 무작위로 5개 입력 받기 -> array로 관리 (list x)
// 2. 그 숫자들이 홀수인지, 짝수인지 출력하기
// 3. 짝수인 경우에는 그 숫자의 절반까지 카운트 하기
//
// array, when 사용하기
fun kotlin7() {
println("게임을 시작합니다. 정수를 5번 입력하세요")
var gameArray = Array<Int>(size = 5, init = { it + 1 })
for (index in 0 .. 4) {
val reader = Scanner(System.`in`)
gameArray.set(index, reader.nextInt())
}
gameArray.forEachIndexed{ index, value ->
println("${index}번째 입력한 수:${value} ")
printNumber(value)
printCountdown(value)
}
}
// 짝수 판독기
public fun isEven(num: Int): Boolean {
return num % 2 == 0
}
// 짝홀 출력기
fun printNumber(num: Int) {
val result = isEven(num)
if (result) {
println("$num 은 짝수입니다.\n")
} else {
println("$num 은 홀수입니다.\n")
}
}
// 카운터 출력기
fun printCountdown(num: Int) {
val result = isEven(num)
if (!result) {
return
}
val target = num/2
for (index in 0..<target) {
println(target - index)
}
println("-----------end------------\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment