+6 반복문(2차 for, while)
java
변수/연산자/제어문
객체지향/예외처리/라이브러리
-- 메소드
oracle
DCML/DDL/TCL/PS-SQL
JSP
JSTL/EL/MVC
Spring
DI/AOP/Transaction
AWS
배포
-- 여기까지는 누구나 다 해서 다른 경쟁력이 필요함
그래서 최신기술 추가
Spring-boot, JPA, VueJS, React, MySQL
반복문_1
구구단
public static void main(String[] args) {
for(int i=1; i<=9; i+=2)
{
for(int j=2; j<=9; j+=2)
{
System.out.printf("%2d * %2d=%2d\t", j,i,j*i);
}
System.out.println();
}
}
반복문_2
별 찍기
/*
* ☆☆☆☆☆
* ☆☆☆☆☆
* ☆☆☆☆☆
* ☆☆☆☆☆
* ☆☆☆☆☆
*
* i = 1-5 (세로, 1차)
* j = 1-5 (가로, 2차)
*/
public static void main(String[] args) {
for(int i=1; i<=5; i++)
{
for(int j=1; j<=5; j++)
{
System.out.print("☆");
}
System.out.println();
}
}
별 찍기_2
/*
* ★☆☆☆☆
* ☆★☆☆☆
* ☆☆★☆☆
* ☆☆☆★☆
* ☆☆☆☆★
*
* i = 1-5 (세로, 1차)
* j = 1-5 (가로, 2차)
*/
public static void main(String[] args) {
for(int i=1; i<=5; i++)
{
for(int j=1; j<=5; j++)
{
if(i==j) // i와 j의 순서가 같을 때!!
System.out.print("★");
else
System.out.print("☆");
}
System.out.println();
}
}
별 찍기_3
/*
* ☆☆☆☆★
* ☆☆☆★☆
* ☆☆★☆☆
* ☆★☆☆☆
* ★☆☆☆☆
*
* i j
* 1 5 → i+j=6
* 2 4
* 3 3
* 4 2
* 5 1
*
*/
public static void main(String[] args) {
for(int i=1; i<=5; i++)
{
for(int j=1; j<=5; j++)
{
if(i+j == 6) // i와 j의 순서의 합이 6일 때!!
System.out.print("★");
else
System.out.print("☆");
}
System.out.println();
}
}
반복문_3
알파벳 출력
/*
* ABCDE
* FGHIJ
* KLMNO
* PQRST
* UVWXY
*/
public static void main(String[] args) {
char c='A';
for(int i=1; i<=5; i++)
{
for(int j=1; j<=5; j++)
{
System.out.print(c++);
}
System.out.println();
}
}
알파벳 출력_2
/*
* ABCDE
* ABCDE
* ABCDE
* ABCDE
* ABCDE
*/
public static void main(String[] args) {
for(int i=1; i<=5; i++)
{
char c='A'; // 변수 위치 변경
for(int j=1; j<=5; j++)
{
System.out.print(c++);
}
System.out.println();
}
}
변수 위치에 따라 값 변하는 거 숙지 잘 해야겠다
반복문_4
순서 있는 별 찍기
/*
* **
* ***
* **
* ***
* **
* ***
*
*/
public static void main(String[] args) {
for(int i=1; i<=6; i++) // 세로!!
{
if(i%2==0)
System.out.println("***");
else
System.out.println("**");
}
}
반복문_5
순서 있는 별 찍기_2
/*
* ☆
* ☆☆
* ☆☆☆
* ☆☆☆☆
*
*/
public static void main(String[] args) {
for(int i=1; i<=4; i++)
{
for(int j=1; j<=i; j++) // j가 i만큼 증가
{
System.out.print("☆");
}
System.out.println();
}
}
순서 있는 별 찍기_3
/*
* ☆☆☆☆
* ☆☆☆
* ☆☆
* ☆
*
* i j
* 1 4 → i+j=5
* 2 3
* 3 2
* 4 1
*
*/
public static void main(String[] args) {
for(int i=1; i<=4; i++)
{
for(int j=1; j<=5-i; j++) // 위 식으로 어떻게든 하기..?!
{
System.out.print("☆");
}
System.out.println();
}
}
입력한 만큼 별 출력
정방향
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("정수 입력: ");
int num = sc.nextInt();
for(int i=1; i<=num; i++)
{
for(int j=1; j<=i; j++) // j가 i만큼 증가
{
System.out.print("☆");
}
System.out.println();
}
}
역방향
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("정수 입력: ");
int num = sc.nextInt();
for(int i=1; i<=num; i++)
{
for(int j=1; j<=(num+1)-i; j++) // (num+1)-i = 이건 뭐
{
System.out.print("☆");
}
System.out.println();
}
}
반복문_6
경우의 수_주사위
/*
* 주사위 2개의 합이 6이 될 경우의 수 → 5개
*
* [1,5]
* [2,4]
* [3,3]
* [4,2]
* [5,1]
*/
public static void main(String[] args) {
for(int i=1; i<=6; i++) // 첫 번째 주사위
{
for(int j=1; j<=6; j++) // 두 번째 주사위
{
if(i+j == 6)
{
System.out.printf("[%d,%d]\n", i, j);
}
}
}
입력한 만큼 경우의 수_주사위
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("정수 입력(2-12): "); // 2개 주사위 합친 값
int num = sc.nextInt();
for(int i=1; i<=6; i++) // 첫 번째 주사위
{
for(int j=1; j<=6; j++) // 두 번째 주사위
{
if(i+j == num) // 2개 주사위 합친 값
{
System.out.printf("[%d,%d]\n", i, j);
}
}
}
별 찍기
public static void main(String[] args) {
for(int i=1; i<=4; i++)
{
// 공백
for(int j=1; j<=4-i; j++)
{
System.out.print(" ");
}
// 별표
for(int k=1; k<=i; k++)
{
System.out.print("*");
}
System.out.println();
}
public static void main(String[] args) {
for(int i=1; i<=4; i++)
{
// 공백
for(int j=1; j<=4-i; j++)
{
System.out.print(" ");
}
// 별표
for(int k=1; k<=2*i-1; k++) // 홀수
{
System.out.print("*");
}
System.out.println();
}
1-10까지 출력
for
for(int i=1; i<=10; i++)
{
System.out.println("i=" + i);
}
while
int i=1;
while(i<=10)
{
System.out.println("i+" + i);
i++;
}
do~while
true든 false든 한 번은 출력한다.
int i = 1;
do
{
System.out.println("i=" + i);
i++;
} while (i<=10);
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
파일 내용 읽어오기
while,
try~catch
public static void main(String[] args) {
try
{
FileReader fr = new FileReader("C:\\Users\\user\\eclipse-workspace\\test\\src\\main\\java\\test\\Three.java");
int i=0;
while((i=fr.read())!=-1)
{
System.out.print((char)i);
}
} catch(Exception ex) {}
}
while
알파벳 출력하기
public static void main(String[] args) {
char c = 'A'; // 초기 값과
while(c<='Z') // 범위 잘 주기
{
System.out.print(c++);
}
}
ABCDEFGHIJKLMNOPQRSTUVWXYZ
4시 숙제...
do~while
// 1-10까지 출력 → 짝수만
// do ~ while
public static void main(String[] args) {
int i=1;
do{
if(i%2==0)
System.out.println(i);
i++;
} while (i<=10);
}
break: 반복문 중단
continue
: 특정 부분 제외
: 반복문에서만 사용 가능
break, continue: 자신의 반복문만 제어 가능하다
if(i%2==0)
continue; → 짝수를 제외하고 출력
break
public static void main(String[] args) {
for(int i=1; i<=10; i++)
{
if(i==5)
break;
System.out.println("i=" + i);
}
}
i=1
i=2
i=3
i=4
continue
MVC 구조에 사용 됨
public static void main(String[] args) {
for(int i=1; i<=10; i++)
{
if(i==3 | i==4 || i==5) // 제외하고 출력한다
continue;
System.out.println("i=" + i);
}
}
i=1
i=2
i=6
i=7
i=8
i=9
i=10
while 예제
1. 2+4+5..100까지 정수 합 구하고 출력하기
public static void main(String[] args) {
int i=2;
int sum=0; // 합을 누적하는 변수
while(i<=100) {
sum+=i;
i+=2; // 2마디씩
}
System.out.println("2+4+6..100까지의 정수의 합: " + sum);
}
2. 5, 10, 15..50을 출력하는 프로그램 만들기
5 10 15 20 25 30 35 40 45 50
// 강사 방식
public static void main(String[] args) {
int i=5;
while(i<=50) {
System.out.print(i+ " ");
i+=5;
}
}
// 내 방식
public static void main(String[] args) {
int i=0;
while(i<=50) {
i++;
if(i%5==0)
System.out.print(i + " ");
}
}
3. B, D, F, H, J, L, N을 출력하는 프로그램 만들기
B D F H J L N
// 강사 방식
public static void main(String[] args) {
char c = 'B';
while(c <= 'N') {
System.out.print(c + " ");
c+=2;
}
}
public static void main(String[] args) {
char c='A';
while(c<='N') {
c++;
if(c%2==0)
System.out.print(c + " ");
}
}
4. 한 개의 정수를 입력 받아 1부터 입력 받은 정수까지의 합 출력하기
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("정수 입력: ");
int num = sc.nextInt();
if(num == 000) {
System.out.println("프로그램 종료");
break;
}
if(num<1 || num>100) {
System.out.println("잘못된 입력입니다!!");
continue;
}
// for
int sum=0;
for(int i=1; i<=num; i++) {
sum+=i;
}
// while
int sum=0;
int i=1;
while(i<=num) {
sum+=i;
i++;
}
System.out.println("1-" + num + "까지의 합: " + sum);
}
5. 1부터 30까지의 정수에서 짝수만 한 줄에 3개씩 출력하기
페이지 나눌 때 사용 됨
public static void main(String[] args) {
int i=1; // 1-30
int j=0; // 3개씩 나눠주는 변수
while (i<=30) {
if(i%2==0) {
if(j%3==0 && j!=0) {
// 3개 나누기
// j!=0 처음 한 칸 지우기
System.out.println(); // 3개 출력하고 다음에 출력
j=0; // 용도?
}
System.out.printf("%2d\t", i);
j++; // j가 3번 돌았는지 확인
}
i++;
}
}
}
6. 1-2+3-4+5-6+7-8+9-10 계산 값을 while문을 이용해 출력하기
public static void main(String[] args) {
int i=1;
int sum=0;
while(i<=10) {
if(i%2==0)
sum-=i;
else
sum+=i;
i++;
}
System.out.println("1-2+3..-10까지의 합: " + sum);
}
7. 1~10 사이의 숫자 중 3의 배수를 제외하고 출력하기 (continue 사용)
public static void main(String[] args) {
int i=1;
while(i<=10) {
if(i%3==0) // 3의 배수
{
i++; // while문에서 continue 쓰는 방법, '증가하고' continue
continue; // 제외한다
}
System.out.print(i+ " ");
i++; // 안 붙이면 무한 루프 됨
}
}
8. while문으로 UPDOWN 게임
1-100 사이의 정수 입력:
50
입력 값보다 작은 값 입력!!
1-100 사이의 정수 입력:
25
입력 값보다 큰 값 입력!!
1-100 사이의 정수 입력:
30
Game Over!!
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 난수 발생
int com = (int)(Math.random()*100)+1; // 1-100
while(true) {
System.out.println("1-100 사이의 정수 입력: ");
int user = sc.nextInt();
// 오류 처리 → 유효성 검사(웹)
if(user<1 || user>100) {
System.out.println("잘못된 입력입니다!!");
continue; // while문 처음으로 돌아감
}
if(com>user) {
System.out.println("입력 값보다 큰 값 입력!!");
}
else if(com<user) {
System.out.println("입력 값보다 작은 값 입력!!");
}
else {
System.out.println("Game Over!!");
break; // while문 종료, 게임 종료
}
}