JAVA/P
예외처리 연습 : 두가지 범위 설정하기 [ 영문과 숫자만 입력가능 / ID ]
꿈꾸는토끼
2020. 9. 29. 11:50
1.
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
26
27
28
29
30
31
32
33
34
35
36
|
Scanner sc = new Scanner(System.in);
String id;
String small;
boolean b;
while(true) {
b=false;
System.out.print("ID 입력 : ");
id=sc.next();
small=id.toLowerCase(); // 대소문자 구별을 위해 소문자로 바꿔줌
for(int i=0 ; i<small.length() ; i++) {
char a = small.charAt(i);
int asc = (int)a; // 아스키코드로 변경
if ( asc<97 || asc>122 ) {
System.out.println("영문/숫자만 입력하세요");
b=true;
break;
}
else if (asc>48 || asc<58) { // 숫자는
b=false;
break;
}
}
if(b==false) {
System.out.println("입력된 ID는 " + id);
break;
}
|
cs |
2.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | // 영어만 입력받는 거 만들어보기 + 숫자까지 (영문과숫자로이루어진id입력받기) // ID Scanner sc = new Scanner(System.in); String id; String small; boolean b; while(true) { b=false; System.out.print("ID 입력 : "); id=sc.next(); small=id.toLowerCase(); // 대소문자 구별을 위해 소문자로 바꿔줌 for(int i=0 ; i<small.length() ; i++) { char a = small.charAt(i); int asc = (int)a; // 아스키코드로 변경 if ((96<asc && asc<123) || (47<asc && asc<58) ) { b=false; break; } else { System.out.println("영문/숫자만 입력하세요"); b=true; break; } } if(b==false) { System.out.println("입력된 ID는 " + id); break; } } | cs |