원쥬
원주는 공부중
원쥬
전체 방문자
오늘
어제
  • 분류 전체보기 (29)
    • Django (0)
    • Vue3 (2)
    • Android (7)
    • Arduino (0)
    • JSPServlet (0)
    • CSS (0)
    • HTML (0)
    • Java (9)
      • Java (9)
      • JavaFestival (0)
    • JavaScript (0)
    • Machine Learning (5)
    • Python (2)
    • Project (1)
      • first_project (1)
      • second_project (0)
      • third_project (0)
    • Tistory (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • matplotlib
  • while
  • Vue3
  • while문
  • machine learning
  • 영화데이터
  • JSON
  • Python
  • android
  • pandas
  • v-on
  • v-model
  • invisible
  • while문 예제
  • 안드로이드
  • machinelearning
  • 손글씨데이터
  • button
  • textarea 오른쪽 하단
  • v-bind
  • 사라지게하기
  • visible
  • 폰트바꾸기
  • API
  • volley
  • 안드로이드 스튜디오
  • Ref
  • AndroidStudio
  • Android Stuido
  • vscode

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
원쥬

원주는 공부중

[Java] 반복문 while/do-while
Java/Java

[Java] 반복문 while/do-while

2022. 7. 28. 01:24

반복문

- 같은 동작을 반복하는 명령어

- 똑같은 내용을 계속해서 출력해야하거나 같은 동작을 계속 반복해야 할 때

- 어떤 조건에 만족할 때 까지 같은 처리를 반복하여 실행하는 구조

 

반복문을 사용하는 이유

- 특정한 명령을 반복적으로 사용하기 위해서

- 코드의 간소화 

- 일반적으로 프로그래밍에서 중복되는 코드를 최대한 줄이고 간단하게 작성하는 것이 최우선!

while문

- 반복 횟수가 정해지지 않은 경우 , 명확하지 않을 때

 

while문의 구조

 

while( 조건식 ) {

	// 조건식 결과가 true일 동안 실행
	실행문장;

}

while문 예제 1)

- while(true) -> 무한 반복

- while문의 핵심은 반복되는 구간을 찾는 것

package while문;

import java.util.Scanner;

public class Ex0110보다작은수입력 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		while (true) {
			System.out.print("정수입력 : ");
			int num = sc.nextInt();
		
			if (num > 10) {
				System.out.println("종료되었습니다.");
				break;
			}
		}
		
	
	}

}

while문 예제 2)

package while문;

import java.util.Scanner;

public class Ex02누적프로그램 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int sum = 0;
		
		while (true) {

			System.out.print("숫자 입력 : ");
			int num = sc.nextInt();

			if (num == -1) {
				sum += num;
				System.out.println("누적결과 : " + sum);
				System.out.println("종료되었습니다.");
				break;
				
			} else {
				sum += num;
				System.out.println("누적결과 : " + sum);
			}

		}

	}

}

while문 예제 3)

package while문;

import java.util.Scanner;

public class ex01while문 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		
		int even = 0;
		int odd = 0;
		
		while (true) {
			
			System.out.print("숫자 입력 : ");
			int num = sc.nextInt();
			
			if (num%2 == 0) {
				
				even++;

				
			} else if (num%2 == 1) {
				
				odd++;
				
			} else if (num == -1) {
				
				System.out.println("종료되었습니다.");
				break;
				
			}
			
			System.out.println("짝수 개수 : " + even);
			System.out.println("홀수 개수 : " + odd);
			
			
		}
		
		
		
		
	}

}

 

do - while문

- 반복 횟수가 정해지지 않은 경우 , 명확하지 않을 때

do {
	// 반드시 한번은 실행되어야 하는 문장
    실행문장1;
 // 그 후 조건식 결과가 true일 동안 실행됨

} while(조건식);

 

do-while문 예제 1)

package 반복문;

import java.util.Scanner;

public class ex01dowhile {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		
		System.out.print("현재 몸무게 : ");
		int weight = sc.nextInt();
		
		System.out.print("목표 몸무게 : ");
		int goal = sc.nextInt();
		
		int minus= 0;
		int week = 0;
		
		do {
			
			week++;
			System.out.print(week + "주차 감량 몸무게 : ");
			minus = sc.nextInt();
			weight -= minus;
			
		} while(weight >  goal);
		
		System.out.println(weight+ "kg 달성 !! 축하합니다!! ");
		
		
	}

}

do-while문 예제2)

package 반복문;

import java.util.Scanner;

public class ex04로그인프로그램 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		
		String id = "Hello";
		String pw = "1234";
		
		String sc_id = "";
		String sc_pw = "";
		
		do {
		System.out.print("아이디를 입력해 주세요 >> ");
		sc_id = sc.next();
		
		System.out.print("비밀번호를 입력해 주세요 >> ");
		sc_pw = sc.next();
		
		if (id.equals(sc_id) && pw.equals(sc_pw)) {
			
			System.out.println("로그인 성공!");
			break;
		} else {
			
			System.out.println("아이디와 비밀번호가 잘못되었습니다.");
		}
		
		while (!id.equals(sc_id) || !pw.equals(sc_pw));
		
	}

}

 

저작자표시 (새창열림)

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

[Java] 반복문 for문  (0) 2022.08.02
[Java] while문 예제 로그인 프로그램/계산기 프로그램/ 플러스게임  (0) 2022.08.02
[Java] 조건문 응용 실습 - 자판기 프로그램 if/ switch-case  (0) 2022.07.24
[Java] 제어문/ 조건문  (0) 2022.07.24
[Java] 입출력문과 연산자  (0) 2022.07.23
    원쥬
    원쥬
    Git : https://github.com/wonjuju/

    티스토리툴바