JAVA

while 문 do while 문 for문

꾸준히개발하자 2020. 8. 10. 12:27
import java.util.Scanner;


	
public class Ex09_Statement {
	public static void main(String[] args) {
		System.out.println("입력");
		Scanner sc = new Scanner(System.in); // 사용자가 입력하면 값을 처리함
		
		/*
		if(sc.hasNextInt()) { // 의문 ( true , false )
			// 정수가 입력되었어
		} else {
			// 정수가 입력되지 않았어
		}
		*/
		// 암기
		// 조건문 : if문 3가지   switch(조건) {case 값: break} 
		// 반복문 : for(반복횟수가 명확) , while(true,false){} , do ~ while(){}
		// 분기문 : break : 블록 탈출 , continue : 구문을 스킵한다.
		
		int count = 0;
		if(count < 1) System.out.println("true");
		if(count < 1) System.out.println("true");
		char data = 'A'; // 캐릭터는 정수값과 호환이 된다.
		
		switch (data) {
		case 'A': System.out.println(); // 콜론 뒤에가 실행 문이다. 문자 비교가 가능하다. 
			break; // switch 문을 탈출해라 
		default: // 나머지 모든것 
			break;
		}
		
		// 1부터 100까지의 합 출력
		int sum = 0;
		for(int i=1; i<=100; i++) {
			if(i % 2 == 0)
			sum += i;
		}
		System.out.println(sum);
						// 1 ~ 5 까지의 합
						// n * (a + 1) / 2
						// 개수 * (시작 + 끝) / 2
						sum = 5 * ( 1 + 5 ) / 2;
		System.out.println("sum : " + sum);
			}
	// for문을 사용해서 1부터 10까지의 홀수의 합을 구하세요

	}

public class Ex09_Statement2 {
	public static void main(String[] args) {
		
		// for문을 사용하여 1~10까지의 홀수의 합을 구하세요. 단 if문 금지
		int sum=0;
		for(int i=1; i<=10; i+=2) { // i = i + 2;
			sum += i;
			System.out.println(i); 
		}
		System.out.println(sum); 
		// 1~100까지의 합(짝수) if문 사용
		int sum3 = 0;
		for(int i=1; i<=100; i++) {
			if(i % 2 == 0) {
				sum3 += i;
				}
			}
		System.out.println(sum3); // 2550 
		// 입사시험 (중소)
		// 구구단 출력하기
		for(int i=2; i<=9; i++) {
			for(int j=1; j<=9; j++) {
				System.out.println(i + " x " + j + " = " + i * j);
				// System.our.printf("[%d] * [%d] = [%d] \t " , i , j , i*j);
				}
			  System.out.println("");
			}
		// 분기문 (continue , break) 
		
		// Today Point : continue( 아래 구문 skip)  break(블럭 for , while)
		for(int i=2; i<=9; i++) {
			for(int j=1; j<=9; j++) {
				if(i == j) { // 3 x 3 같은건 출력하지 않는다. 
					continue; 
					}
				System.out.println(i + " x " + j + " = " + i * j);
			   }
			System.out.println("");
		}
		for(int i=100; i>=0; i--) {
			System.out.println(i);
		}
		
		// 피보나치 수열  1 1 2 3 5 8 13 21 
		int a = 0 , b = 1 , c = 0;
		for(int i =0; i<10; i++) {
			a=b;
			b=c;
			c=a+b;
			System.out.println(" " + c); // 1 1 2 3 5 8 13 21 
		}
	}
}

 

 

 

 

public class Ex10_Statement {

	public static void main(String[] args) {
		// 반복문 (while , do ~ while)
		int i = 10;
		while(i >= 10) {
			System.out.println("i " + i);
			i--; // while문은 반드시 증가감 논리 필요하다  안주면 무한 루프에 빠진다.
		}
		
		// 1부터 100까지 더해주는 while문
		int sum = 0;
		int j = 1;
		while(j <= 100) { // true일 경우만 실행한다. // sum : 5150 주의 사항 
			sum += j;
			j++; // 주로 증가감소를 뒤에 넣는다
			}
			System.out.println("sum : " + sum);
			// while문으로 구구단 출력하기
			
			int k =2;
			int m =1;
			while( k <= 9) {
				m=1; // 2번쨰 while문을 초기화 시켜줘야 2번째 while문이 true라서 들어갈수있다. 
				while( m <= 9) {
					System.out.println(k * m);
					m++;
				}
				System.out.println("****************************");
				k++;
			}			
		}
	}

 

import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) {
		
		int i = 2;	
		
		while(i <= 9) {
			int j = 1;
			while(j <= 9) {
				System.out.println(i * j);
				j++;
			}
			i++;
		}
		
		while(true) { 
			if(true) break; // while문도 if문도 넣을수있고 break도 넣을수있다.
		}
		
		
		// do ~ while : 강제적 수행 ... 무조건 한번은 실행 되게 하겠다.
		// do { 실행문  } while (조건) 
		// 메뉴 구성 
		// 1. 인사 
		// 2. 회계
		// 선택
		// 당신이 원하는 업무를 선택하세요  >> do { 선택하세요 .. 값을 받아서 }
		// 입력값 : 3 >> while(조건식) 
		
		Scanner scanner = new Scanner(System.in);
		int inputdata;
		
		do {
			System.out.println("숫자 입력해(0~9");
			inputdata = Integer.parseInt(scanner.nextLine());
		} while (inputdata >= 10); // 조건이 true 라면 do문을 계속 실행한다. 
								   // 조건이 false 일때 탈출
		System.out.println("당신이 입력한 숫자는  : " + inputdata + " 입니다.");
	}
}