From cc7eda27db15c733ccf79bc0a83779efff989bd9 Mon Sep 17 00:00:00 2001 From: gguGit Date: Sun, 8 Sep 2024 12:15:14 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20fun=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 ++ src/main/java/main.kt | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/main/java/main.kt diff --git a/build.gradle b/build.gradle index 6282254c4ce..e2fd705bb27 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ plugins { id 'java' + id 'org.jetbrains.kotlin.jvm' version '1.7.21' } version '1.0' @@ -11,6 +12,7 @@ repositories { dependencies { testImplementation 'org.assertj:assertj-core:3.22.0' testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2' + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" } java { diff --git a/src/main/java/main.kt b/src/main/java/main.kt new file mode 100644 index 00000000000..138e7133c09 --- /dev/null +++ b/src/main/java/main.kt @@ -0,0 +1,22 @@ +import kotlin.random.Random + +data class Car(val name: String="", var pos: Int=0) + +fun main(){ + //자동차 이름 입력 + val cars: Array = inputCarName() + + //횟수 입력 + val trialCnt = inputTrialCnt() +} + +fun inputCarName(): Array{ //자동차 명 입력 + print("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)\n") + val carNames = readLine()?.split(",") ?: throw IllegalArgumentException("[ERROR]: 자동차 이름을 입력해 주세요") + return carNames.map{ Car(it,0)}.toTypedArray() +} + +fun inputTrialCnt(): Int{ //시도 횟수 입력 + print("시도할 횟수는 몇회인가요?\n") + return readLine()?.toInt() ?: throw IllegalArgumentException("[ERROR]: 횟수를 입력해주세요") +} \ No newline at end of file From 9b1ac55a36fde2dcbfc42b3e4f1960e50a09db82 Mon Sep 17 00:00:00 2001 From: gguGit Date: Sun, 8 Sep 2024 12:19:17 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=A0=84?= =?UTF-8?q?=EC=A7=84=EC=97=AC=EB=B6=80=20=EA=B3=84=EC=82=B0=20=EB=B0=8F=20?= =?UTF-8?q?=EA=B2=BD=EC=A3=BC=20fun=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/main.kt | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/main.kt b/src/main/java/main.kt index 138e7133c09..ea722783e70 100644 --- a/src/main/java/main.kt +++ b/src/main/java/main.kt @@ -8,6 +8,8 @@ fun main(){ //횟수 입력 val trialCnt = inputTrialCnt() + + race(cars,trialCnt) } fun inputCarName(): Array{ //자동차 명 입력 @@ -19,4 +21,22 @@ fun inputCarName(): Array{ //자동차 명 입력 fun inputTrialCnt(): Int{ //시도 횟수 입력 print("시도할 횟수는 몇회인가요?\n") return readLine()?.toInt() ?: throw IllegalArgumentException("[ERROR]: 횟수를 입력해주세요") +} + +fun calculateCarMovement(car: Car){ //전진 판단 계산 + //랜덤 값 생성 후 4이상이면 전진 + if(4 <= Random.nextInt(10)) + car.pos++ +} + +fun race(cars: Array, trialCnt: Int){ //경주 + println("실행 결과") + repeat(trialCnt){ + cars.forEach { car-> + calculateCarMovement(car) + + println("${car.name}, ${"-".repeat(car.pos)}") + } + println() + } } \ No newline at end of file From 8082e775fb42323d4b0004679682f862353f77ef Mon Sep 17 00:00:00 2001 From: gguGit Date: Sun, 8 Sep 2024 12:33:25 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=EC=B5=9C=EC=A2=85=EC=9A=B0=EC=8A=B9?= =?UTF-8?q?=EC=9E=90=20=EC=B6=9C=EB=A0=A5=20fun=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/main.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/main.kt b/src/main/java/main.kt index ea722783e70..1aaacc3645d 100644 --- a/src/main/java/main.kt +++ b/src/main/java/main.kt @@ -10,6 +10,8 @@ fun main(){ val trialCnt = inputTrialCnt() race(cars,trialCnt) + + printWinnerCar(cars) } fun inputCarName(): Array{ //자동차 명 입력 @@ -39,4 +41,12 @@ fun race(cars: Array, trialCnt: Int){ //경주 } println() } + println() +} + +fun printWinnerCar(cars: Array){ + val maxPos = cars.maxByOrNull { it.pos }?.pos + val winners = cars.filter {it.pos == maxPos}.joinToString(", ") { it.name } + + println("최종 우승자 : ${winners}") } \ No newline at end of file