scanner 입력이 버퍼 때문에 잘 안될때

2020. 1. 30. 17:01문제들

buffer에 들어온 enter 때문에 입력창만 반복해서 뜨곤 한다.

이럴때는 Scanner.nextLine()을 통해 buffer를 비워준다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        while(!(region >= 1 && region<= 6)) {
            try{
                System.out.println("전염병을 퍼뜨릴 곳을 선정하세요");
                System.out.println("1.아프리카");
                System.out.println("2.유럽");
                System.out.println("3.오세아니아");
                System.out.println("4.아시아");
                System.out.println("5.남아메리카");
                System.out.println("6.북아메리카");
                System.out.println("입력 : ");
                region = scan.nextInt();    
            }catch(Exception e){            
                scan.nextLine(); // buffer 나머지 부분을 비워준다
                region = 0
                System.out.println("올바른 값을 입력하세요");
            }
        }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
sdfsdfasdaddassdfsdf