Binary World

JAVA CLASS : 반복문(for, while, loop) 본문

개발자의 길/JAVA

JAVA CLASS : 반복문(for, while, loop)

모쿠 2016. 12. 29. 11:31

<Java Practice>


* ForMain01(for반복문)


public class ForMain01 {


public static void main(String[] args) {

System.out.println("for 반복문");

for(int x=1; x <= 10; x++){

System.out.println("Java" + x);

}

System.out.println("==================");

// 1, 2, 3, 4, .. 10 출력

for (int i=1; i <=10; i++){

System.out.println(i);

}


System.out.println("==================");

// 10, 9, 8, 7 출력

for (int i=10; i >= 1; i--){

System.out.println(i);

}

} // end main()


} // end class ForMain01 



* ForMain02(for문으로 짝수 출력)


public class ForMain02 {


public static void main(String[] args) {

// 10 이하의 짝수만 오름 차순으로 출력

for (int i = 0; i <= 5; i++){

System.out.println(2*i);

} // end for


System.out.println("===============");

// 10, 8, 6, 4, 2, 0 출력

for (int x = 10; x >= 0; x -= 2){

System.out.println(x);

} // end for

for (int y = 0; y <= 10; y++){

if((y % 2) == 0 ){

System.out.println(y);

} // end if

} //end for

} // end main()


} // end class ForMain02



*Formain04(특정 위치에서 줄바꾸기)


public class ForMain04 {


public static void main(String[] args) {

// 1 ~ 100까지 정수중에서 9의 배수들만 출력

for (int y = 1; y <= 100; y++){

if(y % 9 == 0 ){

System.out.println(y);

} // end if

} //end for

System.out.println("============");

// 1 ~ 100까지 숫자들 출력

// 1 2 3 ... 10

// 11 12 13 ... 20

// ....

// 91 92 93 .... 100

for(int i = 1; i <= 100; i++){

System.out.print(i +" ");

if(i % 10 == 0){

System.out.println();

}

}

} //end main()




* ForMain05(for문과 if문 사용)


public class ForMain05 {

public static void main(String[] args){

// 1꼬마 2꼬마 3꼬마 인디언

// 4꼬마 5꼬마 6꼬마 인디언

// 7꼬마 8꼬마 9꼬마 인디언

// 10꼬마 인디언 보~이

for(int i = 1; i <= 10; i++){

System.out.print(i + "꼬마 ");

if(i % 3 == 0){

System.out.print("인디언\n");

} else if(i % 10 == 0 ){

System.out.println("인디언 보이~");

}

}

}




* Formain05(등차수열의 합)


public class ForMain06 {


public static void main(String[] args) {

// 1 + 2 + 3 + .... + 10

int sum1 = 0;

for(int i = 1; i<=10; i++){

sum1 = sum1 + i;

System.out.println("i = " + i + ", sum1= " + sum1);

}

System.out.println();

System.out.println("sum = " + sum1);

System.out.println("================");

// 1 ~ 100 까지 짝수들의 합을 출력

// sum = 2 + 4 + 6.. + 100

int sum2 = 0;

for (int even = 1; even <= 100; even++ ){

if ( even % 2 == 0){

sum2 = sum2 + even;

System.out.println("even = " + even + ", sum2= " + sum2);

}

}

System.out.println(sum2);

// 1 ~ 100 까지 홀수들의 합을 출력

// sum = 1 + 3 + 5 + .. + 97 + 99

int sum3 = 0;

for (int odd = 1; odd <= 100; odd++ ){

if ( odd % 2 == 1){

sum3 = sum3 + odd;

System.out.println("odd = " + odd + ", sum3= " + sum3);

}

}

System.out.println(sum3);


} // end main()




* WhileMain02 (while 구구단)


public class WhileMain02 {


public static void main(String[] args) {

// 구구단 3단을 while문을 사용해서 출력

int i = 1;

while(i <= 9){

System.out.println("3" + "X" + i + "=" + (3 * i));

i++;

}

System.out.println("===============");

// sum = 1 + 2 + 3 + ... + 9 + 10

int sum = 0;

i = 1;

while (i <= 10){

sum += i;

i++;

}

System.out.println("sum = " + sum);

} // end main()


} // end class WhileMain02 



* WhileMain03 (3의 배수 총합)


public class WhileMain03 {


public static void main(String[] args) {

// 1부터 100까지 중 3의 배수의 총합 구하기(for, while)

int n = 1;

int sum = 0;

//for문

for(n = 1; n <= 100; n ++){

if(n % 3 ==0){

sum += n;

}

}

System.out.println("sum :" + sum);

System.out.println("================");

//while

n = 1;

sum = 0;

while(n <= 100){

if(n % 3 ==0){

sum += n;

}

n++;

}

System.out.println("sum :" + sum);


} // end main()


} // end class WhileMain03 


* 3,6,9 게임


public class HomeworkMain01 {

public static void main(String[] args){

// 3, 6, 9 게임 만들기

// 3, 6, 9 에서 별(*) 출력

int n = 1;

for (n = 1; n <= 100; n ++){

if (n / 10 == 3){

int num = n % 10;

switch (num){

case 3: 

System.out.print("**" + " ");

break;

case 6:

System.out.print("**" + " ");

break;

case 9:

System.out.print("**" + " ");

break;

default :

System.out.print("*" + " ");

}

}

else if (n / 10 == 6){

int num = n % 10;

switch (num){

case 3: 

System.out.print("**" + " ");

break;

case 6:

System.out.print("**" + " ");

break;

case 9:

System.out.print("**" + " ");

break;

default :

System.out.print("*" + " ");

}

}

else if (n / 10 == 9){

int num = n % 10;

switch (num){

case 3: 

System.out.print("**" + " ");

break;

case 6:

System.out.print("**" + " ");

break;

case 9:

System.out.print("**" + " ");

break;

default :

System.out.print("*" + " ");

}

}

else if (n % 10 == 3)

{

System.out.print("*" + " ");

}

else if (n % 10 == 6)

{

System.out.print("*" + " ");

}

else if (n % 10 == 9)

{

System.out.print("*" + " ");

}

else 

{

System.out.print(n + " ");

}

if (n % 10 == 0)

{

System.out.println();

}

}

} // end main()


} // end class Homework01


* WhileMain05(반복문을 통한 조회)


import java.util.Scanner;


public class WhileMain05 {


public static void main(String[] args) {

// while 문과 scanner를 이용해서 예금, 출금, 조회 종료 제공하는 코드 작성

boolean run = true; // while문의 반복 여부 검사

int balance = 0; // 예금 잔고

Scanner sc = new Scanner(System.in);

while (run){

System.out.println("---------------------------");

System.out.println("1.예금 | 2.출금 | 3.잔고 | 4.종료 ");

System.out.println("---------------------------");

System.out.println("선택>");

int input = sc.nextInt();

switch(input){

case 1:

System.out.println("예금액>");

balance += sc.nextInt();

break;

case 2:

System.out.println("출금>");

balance -= sc.nextInt();

break;

case 3:

System.out.println("잔고>" + balance);

break;

case 4:

System.out.println("종료합니다.");

run = false;

break;

default:

System.out.println("올바른 메뉴를 선택하세요.");

}

} // end while

sc.close();

System.out.println("프로그램 종료");

}


}



*WhileMain06


public class WhileMain06 {


public static void main(String[] args) {

System.out.println("while 문");

int count = 0;

while(count > 0){

System.out.println(count);

count--;

} // end while

// count 값이 출력되지 않는다.

do{

System.out.println(count);

count--;

}while(count > 0); // end do-while

// count = 0으로 출력


} // end main()


} // end class



*LoopMain01(break를 이용한 반복문 종료)


public class LoopMain01 {


public static void main(String[] args) {

System.out.println("break");

// 반복문(for, while, do-while) 안에서 break를 만나면

// break 가 있는 위치에서 가장 가까운 곳의 반복문을 종료

for (int n = 1; n <= 10; n++) {

System.out.println(n);

if (n == 5){

break;

} // end if

} // end for

System.out.println("===================");

int n = 1;

while( n <= 10) {

System.out.println(n);

if (n == 5){

break; // 반복문(while) 종료

}

n++;

} // end while

// 7의 배수를 출력하는데 100보다 작은 7의 배수를 출력

System.out.println("===================");

n = 1;

while(true){

int seven = 7*n;

if (seven > 100){

break;

}

System.out.println(seven);

n++;

}

} // end main()


} // end class 


* LoopMain04(이중반복문)


public class LoopMain04 {


public static void main(String[] args) {

System.out.println("이중 반복문");

int dan;

for (dan = 2; dan < 10; dan++) {

for (int n = 1; n < 10; n++) {

System.out.println(dan + " x " + n + " = " + (dan * n));

} // end for(n)

} // end for(dan)


System.out.println("\n=================\n");

System.out.println("이중 while문");


dan = 2;

int n = 1;

while (n < 10) {

System.out.println(dan + " x " + n + " = " + (dan * n));

n++;

} // end while(n)


System.out.println("----------------");


while (dan < 10) {

n = 1;

while (n < 10) {

System.out.println(dan + " x " + n + " = " + (dan * n));

n++;

} // while(n)

dan++;

} // while(dan)


// Ctrl+Shif+f : 코드 자동 정렬(들여쓰기)


} // end main

} // end class 



*LoopMain06(주사위 숫자 맞추기)


public class LoopMain06 {


public static void main(String[] args) {

// 두 개의 주사위를 던져서 나온 값을(x, y)와 같은 형식으로 출력

// 두 개의 주사위가 같은 숫자가 나오면 종료


int i = 1;

while (true) {

double rX = Math.random(); // 0 <= rX < 1

double rY = Math.random(); // 0 <= rY < 1


int x = (int) (6 * rX + 1);

int y = (int) (6 * rY + 1);

System.out.println(i + "번째 - ");

System.out.println("x : " + x + "," + " y : " + y);

if (x == y) {

System.out.println("같은 수가 나왔습니다.");

break;

} // end if

i++;

} // end while()

System.out.println("---------------");


System.out.println("===================");

// for문을 이용한 무한 루프

i = 1;

for (;;) {

double rX = Math.random(); // 0 <= rX < 1

double rY = Math.random(); // 0 <= rY < 1


int x = (int) (6 * rX + 1);

int y = (int) (6 * rY + 1);

System.out.println(i + "번째 - ");

System.out.println("x : " + x + "," + " y : " + y);

if (x == y) {

System.out.println("같은 수가 나왔습니다.");

break;

} // end if

i++;

} // end for

System.out.println("===================");

// do-while문

int x = 0;

int y = 0;

do {

x = (int) (Math.random() * 6 + 1);

y = (int) (Math.random() * 6 + 1);

System.out.println("(" + x + ", " + y + ")");

} while(x != y);//실행 뒤 조건을 검사하여 참이면 다시 반복

 

} // end main()


} // end main() 



*LoopMain07(무한루프)


public class LoopMain07 {


public static void main(String[] args) {

// sum = 1 + 2 + 3 + ... + 99 + 100

int sum = 0;


for (int i = 1; i <= 100; i++) {

sum += i;

}

System.out.println("sum : " + sum);


System.out.println("================");

// sum = 1 + 2 + 3 + ... >= 100

// n의 값과 sum의 값을 출력

sum = 0;

int n = 1;

for (;;) {

sum = sum + n;

if (sum >= 100) {

break;

}

n++;

}

System.out.println("sum : " + sum);

System.out.println("n : " + n);


} // end main()


} // end class

 









'개발자의 길 > JAVA' 카테고리의 다른 글

JAVA CLASS : 연습(반복문)  (0) 2016.12.29
JAVA CLASS : 배열  (0) 2016.12.29
JAVA CLASS : 조건문(if)  (0) 2016.12.29
JAVA CLASS : 연산자  (0) 2016.12.29
JAVA CLASS : 데이터 타입  (0) 2016.12.29
Comments