Eggs Sunny Side Up
본문 바로가기
언어/JAVA

배열_split()을 이용해 문자열 값에 해당하는 개수를 출력

by guswn100059 2023. 3. 1.

package 배열;

import java.util.Scanner;

public class Ex_연습장 {

	public static void main(String[] args) {
		
		String score = "A,A,B,C,D,A,C,D,D,D,F";
		
		String[] grade = score.split(",");
		
		int cntA = 0;
		int cntB = 0;
		int cntC = 0;
		int cntD = 0;
		int cntF = 0;
		
		for(int i = 0; i < grade.length; i++) {
			
			if(grade[i].equals("A")) {
				cntA++;
			} 
			else if(grade[i].equals("B")) {
				cntB++;
			}
			else if(grade[i].equals("C")) {
				cntC++;
			}
			else if(grade[i].equals("D")) {
				cntD++;
			}
			else if(grade[i].equals("F")) {
				cntF++;
			}
		}
		
		System.out.println("A : "+cntA+"명");
		System.out.println("B : "+cntB+"명");
		System.out.println("C : "+cntC+"명");
		System.out.println("D : "+cntD+"명");
		System.out.println("F : "+cntF+"명");
		
		
		
	}

}

댓글