※ Scanner클래스의 메소드인 useDelimiter를 이용하여 콤마(,) 문자를 기준으로 next메소드를 사용하여 입력받을 수 있다. 이를 활용하여 학생의 이름과 성적을 입력받는 프로그램을 구현하여라.

 



import java.util.Scanner;

public class Test018
{
	public static void main(String[] args)
	{
		// Scnanner 인스턴스 생성
		Scanner sc = new Scanner(System.in);

		// 주요 변수 선언
		String name;
		int kor, eng, mat, tot;	

		// 연산 및 처리
		System.out.print("이름,국어,영어,수학 입력(',' 구분) : ");
		sc = new Scanner(sc.next()).useDelimiter("\\s*,\\s*");
		//				-----------
		//				조윤상,90,80,70
		
		name = sc.next();		//김승범
		kor = sc.nextInt();		// 90
		eng = sc.nextInt();		// 80
		mat = sc.nextInt();		// 70
		
		tot = kor + eng + mat;

		// 결과 처리
		System.out.println();
		System.out.println(">> 이름 : " + name);
		System.out.println(">> 총점 : " + tot);


	}
}

+ Recent posts