자체임상실험
Inform Restaurant
자체임상실험
전체 방문자
오늘
어제
  • 분류 전체보기 (89)
    • IT정보와 지식 TABLE (1)
      • IT정보 및 꿀팁 (0)
      • Windows (1)
      • Linux (0)
    • Linux TABLE (0)
      • 취약점 점검 스크립트 (0)
    • Developer TABLE (33)
      • Java (33)
      • Java Algorithm (0)
    • DataBase TABLE (1)
      • SQLD-P (1)
    • Cloud TALBE (0)
      • AWS (0)
    • Security TABLE (1)
      • 비박스(bee-box) 취약점 점검 (1)
      • 보안기사 (0)
    • Writer TABLE (42)
      • 기사필사 (42)
      • 서평 (0)
    • Growth TABLE (10)
      • 국민취업지원제도 (4)
      • 국비학원 (6)
    • Life TABLE (0)
      • 자서전 (0)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • 한겨레
  • 배열복사
  • 초장기 주택담보대출
  • 방사능
  • 배열
  • 조건문
  • Java
  • 국민취업지원제도
  • 논설위원
  • 지하공간
  • 해양오염수
  • array
  • KH정보교육원
  • 논설위원필사
  • while
  • 반복문
  • 천자칼럼
  • 문자열비교
  • 국비학원
  • Arrays
  • Switch
  • @
  • 경향신문
  • for
  • if
  • 2차원배열
  • do while
  • 동아일보
  • 조용한 고용
  • 디엠제트

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
자체임상실험

Inform Restaurant

Developer TABLE/Java

반복문 do ~ while

2021. 8. 26. 14:50

do ~ while 문

  • 스코프 { }를 최소한 한 번 이상 반복
  • 사용자 입력을 받는 경우 주로 사용
do {
	// 처음 한 번은 무조건 실행
	// 조건식이 만족할 시 반복 수행
}  while (조건식);

1 ~ 임의의 수(1~10)까지 의 합계 

public void method2() {
	int nRandom = ((int)(Math.random()*10)+1);
	int nSum = 0;
	int i = 0;
    
	do {
		i++;
		nSum += i;
		
		System.out.println("nRandom: " + nRandom);
		System.out.printf("i: %d, nSum: %d\n", i, nSum);
		
	} while(i < nRandom);
    
	System.out.println("Total: " + nSum);
}

출력물

nRandom: 5
i: 1, nSum: 1
nRandom: 5
i: 2, nSum: 3
nRandom: 5
i: 3, nSum: 6
nRandom: 5
i: 4, nSum: 10
nRandom: 5
i: 5, nSum: 15
Total: 15

다른 클래스에 있는 프로그램 실행하는 프로그램

public void method3() {
	char cMenu = ' ';
	Scanner s = new Scanner(System.in);
	C_For example = new C_For();
	
	do {
		System.out.println();
		System.out.println("============================");
		System.out.println("1. Excute new C_For.method1()");
		System.out.println("2. Excute new C_For.method2()");
		System.out.println("3. Excute new C_For.method3()");
		System.out.println("Q(q).Program Quie");
		System.out.println("============================");
		System.out.println();
		
		System.out.printf("Insert Number> ");
		cMenu = s.nextLine().charAt(0);
		
		switch (cMenu) {
		case '1' :
			System.out.println("[Menu 1]");
			example.method1();
			break;
			
		case '2' :
			System.out.println("[Menu 2]");
			example.method2();
			break;	
			
		case '3' :
			System.out.println("[Menu 3]");
			example.method3();
			break;
			
		case 'q' :
		case 'Q' :
			System.out.println("QUIT!!\n");
			return;
			
		default :
			System.out.println("\n... Try again\n");
		}	
	} while(true);
}

출력문

============================
1. Excute new C_For.method1()
2. Excute new C_For.method2()
3. Excute new C_For.method3()
Q(q).Program Quie
============================
Insert Number> 1
[Menu 1]
Result: 55

============================
1. Excute new C_For.method1()
2. Excute new C_For.method2()
3. Excute new C_For.method3()
Q(q).Program Quie
============================
Insert Number> 2
[Menu 2]
 [ 2 단]		 [ 3 단]		 [ 4 단]		 [ 5 단]		 [ 6 단]		 [ 7 단]		 [ 8 단]		 [ 9 단]		
2 * 1 =  2	3 * 1 =  3	4 * 1 =  4	5 * 1 =  5	6 * 1 =  6	7 * 1 =  7	8 * 1 =  8	9 * 1 =  9	
2 * 2 =  4	3 * 2 =  6	4 * 2 =  8	5 * 2 = 10	6 * 2 = 12	7 * 2 = 14	8 * 2 = 16	9 * 2 = 18	
2 * 3 =  6	3 * 3 =  9	4 * 3 = 12	5 * 3 = 15	6 * 3 = 18	7 * 3 = 21	8 * 3 = 24	9 * 3 = 27	
2 * 4 =  8	3 * 4 = 12	4 * 4 = 16	5 * 4 = 20	6 * 4 = 24	7 * 4 = 28	8 * 4 = 32	9 * 4 = 36	
2 * 5 = 10	3 * 5 = 15	4 * 5 = 20	5 * 5 = 25	6 * 5 = 30	7 * 5 = 35	8 * 5 = 40	9 * 5 = 45	
2 * 6 = 12	3 * 6 = 18	4 * 6 = 24	5 * 6 = 30	6 * 6 = 36	7 * 6 = 42	8 * 6 = 48	9 * 6 = 54	
2 * 7 = 14	3 * 7 = 21	4 * 7 = 28	5 * 7 = 35	6 * 7 = 42	7 * 7 = 49	8 * 7 = 56	9 * 7 = 63	
2 * 8 = 16	3 * 8 = 24	4 * 8 = 32	5 * 8 = 40	6 * 8 = 48	7 * 8 = 56	8 * 8 = 64	9 * 8 = 72	
2 * 9 = 18	3 * 9 = 27	4 * 9 = 36	5 * 9 = 45	6 * 9 = 54	7 * 9 = 63	8 * 9 = 72	9 * 9 = 81	

============================
1. Excute new C_For.method1()
2. Excute new C_For.method2()
3. Excute new C_For.method3()
Q(q).Program Quie
============================
Insert Number> 3
[Menu 3]
===============
 q or Q = Quit
===============
Please Insert key(q or Q): q
==== Bye Bye Bye ====

============================
1. Excute new C_For.method1()
2. Excute new C_For.method2()
3. Excute new C_For.method3()
Q(q).Program Quie
============================
Insert Number> q
QUIT!!
저작자표시 비영리 (새창열림)

'Developer TABLE > Java' 카테고리의 다른 글

난수 생성 Math.random  (0) 2021.08.26
반복문 while  (0) 2021.08.26
중첩 for 문  (0) 2021.08.26
    'Developer TABLE/Java' 카테고리의 다른 글
    • 분기문(break, continue)
    • 난수 생성 Math.random
    • 반복문 while
    • 중첩 for 문
    자체임상실험
    자체임상실험
    생활에 유용한 정보와 일상을 담은 휴식처, Sobremesa 입니다.

    티스토리툴바