read()함수
- 키보드(input stream)로부터 1byte를 읽어서 ASCII 값을 정수(int)로 반환하는 함수
- 읽은 데이터(바이트)가 없다면 –1을 반환하는 함수
- 엔터(Enter)눌러 값을 입력하면 엔터에 의해 ‘\r’‘\n’값이 들어가 keyboard buffer에 남아있다.
└> System.in.skip(System.in.available()); 함수를 사용하여 버퍼에 남아있는 것을 다 지운다.
순서도
- 미리 정의된 기호와 연결선으로 알고리즘이나 프로그램의 논리 혹은 흐름을 그림으로 표현하는 방법
| 기호 | 명칭 | 사용용도 | 기호 | 명칭 | 사용용도 |
| 처리 | 각종 연산, 데이터 이동 등의 처리 | 터미널 | 순서도의 시작과 끝 표시 | ||
| 연결자 | 흐름이 다른 곳과 연결되는 입출구를 나타냄 | 천공카드 | 천공카드의 입출력 | ||
| 입출력 | 테이터의 입력과 출력 | 서류 | 서류를 매체로 하는 입출력 표시 | ||
| 흐름선 | 처리의 흐름과 기호를 연결하는 기능 | 수동입력 | 콘솔에 의한 입력 | ||
| 준비 | 기억장소, 초기값 등 작업의 준비 과정 나타냄 | 반복 | 조건을 만족하면 반복 | ||
| 미리 정의된 처리 | 미리 정의된 처리로 옮길 때 사용 | 디스플레이 | 결과를 모니터로 나타냄 |
별찍기
- **** for( int i = 1 ; i <= 4; i++) { -> 행의 수
- **** for (int j = 1; j <= 4; j++) System.out.print('*'); -> 열(별)의 수
- **** System.out.println();
- **** }
클린코드
1.
| for ( i = (n >= m ? m : n) ; i <= (n >= m ? n : m) ; i++) { sum += i; System.out.printf("%d+",i); } |
| int min = n < m ? n : m; int max = n > m ? n : m; for ( i = min ; i <= max ; i++) { sum += i; System.out.printf("%d+",i); } |
2.
| if(90 <= kor) System.out.println("수"); else if(80 <= kor) System.out.println("우"); else if(70 <= kor) System.out.println("미"); else if(60 <= kor) System.out.println("양"); else System.out.println("가"); -> 유효성검사가 없음 |
| if( 0 > kor || kor > 100) System.out.println("입력 잘못!!!"); else if(90 <= kor) System.out.println("수"); else if(80 <= kor) System.out.println("우"); else if(70 <= kor) System.out.println("미"); else if(60 <= kor) System.out.println("양"); else System.out.println("가"); |
3.
| System.out.println("> 국어 점수를 입력하세요"); do { kor = scanner.nextInt(); if (isCheck = 0 <= kor && kor <= 100) break; System.out.println("> 입력 잘못!!!\n국어 점수를 다시 입력하세요"); } while(!isCheck); |
| do { System.out.println("> 국어 점수 입력? "); kor = scanner.nextInt(); if (isCheck = 0 > kor || kor > 100) System.out.println("입력오류"); }while(isCheck); |
4.
| for ( int x = 5; x >=0; x--) { for (int y=0; y <= 5; y++) { if ( x>y) System.out.print(" "); else System.out.print("*"); } for (int y=4; y >= 0; y--) { if ( x>y) System.out.print(" "); else System.out.print("*"); } System.out.println(); } for ( int x = 1; x <=5; x++) { for (int y=0; y <= 5; y++) { if ( x>y) System.out.print(" "); else System.out.print("*"); } for (int y=4; y >= 0; y--) { if ( x>y) System.out.print(" "); else System.out.print("*"); } System.out.println(); } |
| for ( int x = 1; x <=11; x++) { for (int y=1; y <= 11; y++) { if(x+y>=7 && x-y<=5 && y-x<=5 && x+y<=17) System.out.print('*'); else System.out.print(' '); } System.out.println(); } |
| int direction = 2; for ( int x = 1, d = 1, d2 = 5; x <=11; x++, d += direction, d2 -= direction/2) { for (int y=1; y <= d2; y++) System.out.print(' '); for (int y=1; y <= d; y++) System.out.print('*'); direction = x == 6 ? -direction : direction; System.out.println(); } |
5.
| for ( int x = 5; x >=0; x--) { for (int y=0; y <= 5; y++) { if ( x>y) System.out.print("*"); else System.out.print(" "); } for (int y=4; y >= 0; y--) { if ( x>y) System.out.print("*"); else System.out.print(" "); } System.out.println(); } for ( int x = 1; x <=5; x++) { for (int y=0; y <= 5; y++) { if ( x>y) System.out.print("*"); else System.out.print(" "); } for (int y=4; y >= 0; y--) { if ( x>y) System.out.print("*"); else System.out.print(" "); } System.out.println(); } |
| for ( int x = 1; x <=11; x++) { for (int y=1; y <= 11; y++) { if (x+y<=6 || x-y>=6 || y-x>=6 || x+y>=18) System.out.print('*'); else System.out.print(' '); } System.out.println(); } |
| int direction = 2; for ( int x = 1, d = 1, d2 = 5; x <=11; x++, d += direction, d2 -= direction/2) { for (int y=1; y <= d2; y++) System.out.print('*'); for (int y=1; y <= d; y++) System.out.print(' '); for (int y=1; y <= d2; y++) System.out.print('*'); direction = x == 6 ? -direction : direction; System.out.println(); } |