본문 바로가기

알고리즘/Java

(JAVA) 백준 알고리즘 6단계 8958번 : OX퀴즈

 

 

소스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Scanner;
public class Main{
    public static void main(String[] args) { 
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.nextLine();
        
        for(int i=0; i<n; i++) {
            String a = scan.nextLine();
            int sum=0;
            int score=0;
            for(int j=0; j<a.length(); j++) {
                if(a.charAt(j)=='O') {
                    score++;
                    sum +=score;
                }else {
                    score=0;
                }
            }
            System.out.println(sum);
        }
             
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

charAt() 은 문장중에 있는 문자를 추출하는 것.

nextLine()은 문자열을 입력받은 것을 리턴.

nextLine()과 next()메소드의 차이는 nextLine()메소드는 Enter를 치기 전까지 쓴 문자열을 모두 리턴한다는 것이고 next() 메소드는 스페이스 즉 공백 전까지 입력받은 문자열을 리턴한다는 것