문제 풀이 ✏️/기타
[백준] 2920 (음계)
interfacer_han
2023. 11. 22. 15:54
#1 코드
#1-1 자바
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String testCase = sc.nextLine().trim().replaceAll("\\s", ""); // \s는 공백 문자를 나타내는 정규 표현식
if(testCase.equals("12345678")) {
System.out.println("ascending");
} else if(testCase.equals("87654321")) {
System.out.println("descending");
} else {
System.out.println("mixed");
}
}
}
자바에서 문자열의 값을 비교할 땐, String 클래스의 equals() 메소드를 써야 함에 주의한다.
#1-2 코틀린
fun main() {
val testCase = readln()!!.trim().replace("\\s".toRegex(), "") // \s는 공백 문자를 나타내는 정규 표현식
if(testCase == "12345678") {
println("ascending")
} else if(testCase == "87654321"){
println("descending")
} else {
println("mixed")
}
}