728x90


hasNext()로 다음 순회할 데이터의 유무 확인하며 반복문을 출력한다.
END 문자열이 나오면 멈춰야한다.
멈춘 후 StringBuilder클래스의 reverse()로 뒤집어 출력해보자.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String [] str = new String [4];
while(sc.hasNext()) {
for(int i = 0; i<str.length; i++) {
str[i] = sc.nextLine();
if(str[i].equals("END")) {
break;
}
StringBuilder sb = new StringBuilder(str[i]);
sb.reverse();
System.out.print(sb);
System.out.println();
}
}
}
}
String 배열을 꼭 4개로 정하지 않고 String str; 이라고만 해도 가능하다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
while (true) {
str = sc.nextLine();
if (str.equals("END"))
break;
}
StringBuilder sb = new StringBuilder(str);
sb.reverse();
System.out.print(sb);
System.out.println();
}
}
}
728x90
'JAVA > 오류고민' 카테고리의 다른 글
[백준] 9093번 단어 뒤집기 (1) | 2023.11.15 |
---|---|
[백준] 17388번 와글와글 숭고한 (1) | 2023.11.15 |
[백준] 11006번 남욱이의 닭장 (0) | 2023.11.07 |
[백준] 10872번 팩토리얼 (0) | 2023.11.07 |
[백준] 10807번 개수 세기 (0) | 2023.11.06 |