Created
October 5, 2023 00:51
-
-
Save rafirh/bb485e76b15d952979a8bec138ca46de to your computer and use it in GitHub Desktop.
Kembara Ke Pulau Harta Karun
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class Solution { | |
public static void main(String[] args) { | |
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ | |
Scanner scanner = new Scanner(System.in); | |
String kodePetunjuk = scanner.nextLine(); | |
String[] bagian = kodePetunjuk.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"); | |
String result = "Kapten Jarjit Singh harus berjalan ke arah "; | |
String arah = ""; | |
int jumlahLangkah = 0; | |
for (int i = 0; i < bagian.length; i++) { | |
if (i % 2 == 0) { | |
arah = konversiArah(bagian[i]); | |
} else { | |
jumlahLangkah = Integer.parseInt(bagian[i]); | |
result += arah + " sebanyak " + jumlahLangkah + " langkah"; | |
if (i == bagian.length - 1) { | |
result += "."; | |
} else if (i == bagian.length - 3) { | |
result += ", dan "; | |
} else { | |
result += ", "; | |
} | |
} | |
} | |
System.out.println(result); | |
} | |
public static String konversiArah(String arah) { | |
switch (arah) { | |
case "U": | |
return "Utara"; | |
case "T": | |
return "Timur"; | |
case "S": | |
return "Selatan"; | |
case "B": | |
return "Barat"; | |
default: | |
return "Arah tidak valid"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment