반응형
NOOWGNAJ
Run To IT
NOOWGNAJ
전체 방문자
오늘
어제
  • 분류 전체보기 (163)
    • Flutter (3)
    • Back (77)
      • JAVA (13)
      • JAVA 응용문제 (8)
      • JSP (16)
      • Spring (5)
      • Python (26)
      • nexacro (3)
      • jstl (6)
    • Database (8)
      • MySQL (6)
      • MSSQL (2)
    • Front (44)
      • HTML (34)
      • CSS (0)
      • JavaScript (5)
      • Vue.js (5)
    • 코딩테스트 (4)
      • SQL (3)
      • JS (1)
    • MUSIC (1)
      • 장운박스 (1)
    • 임시 메모장 (21)
      • 메모장1 (21)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 정규식모음
  • 주민번호정규식
  • 이메일정규식
  • 자바스크립트정규식
  • 자바스크립트
  • regex
  • 핸드폰번호정규식
  • 정규식

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
NOOWGNAJ

Run To IT

Back/JAVA

JAVA Step4-2 (이중 반복문 / 반복문 합계)

2022. 6. 5. 03:55
반응형

이중 for문

[응용문제]

 

결제 최종 금액이 있다.

3개의 상품

"상품 가격을 입력해주세요"

출력, 금액을 입력하고

"할인 %를 입력해주세요"

출력, 할인 %를 입력하면 최종 금액이 변경되어 출력.

import java.util.Scanner;

public class Note {
	public static void main (String [] args){
    
		Scanner sc = new Scanner(System.in);

        
        int re; // 반복
        int total = 0;
        
        for (re=1; re<=3; re++) {
        	System.out.println ("상품 가격을 입력해주세요.");
        	int cost = sc.nextInt();
            total += cost;
        }
		
        System.out.println ("할인 %를 입력해주세요.");
        int coupon = sc.nextInt();
        
        double a = coupon * 0.01;
        double b = a * total;
        double c = total - b;
        
        
        System.out.printf ("상품의 총 합계는 " + total + ", " + coupon + "퍼센트 할인 쿠폰을 사용하여 최종 결제 금액은" + c + "원 입니다.");
        sc.close();
    }
}

for + 

import java.util.Scanner;

public class Note {
	public static void main (String [] args){
    
		int f, ff;
		int total = 0;
		for (f = 1; f <= 3; f++) { // 1 부터 3 반복
			for (ff = 1; ff <= 4; ff++) { // 1 부터 4 반복
				total += f + ff;
				System.out.println(f + " + " + ff + " = ");
    			}
		}
	}
}

// 출력값 :
1 + 1 =
1 + 2 =
1 + 3 =
1 + 4 =
...
2 + 1 =
...
3 + 4 =

이중 while문

import java.util.Scanner;

public class doublewhile {
	public static void main (String [] args){
    
		int a = 1;
		while (a<=3) {
			int b = 1; // 작은 while문의 초기값(반복)을 큰 반복문 안 쪽에 적용.
			while(b<=4) {
				System.out.println(a + " + " + b);
				b++;
			}
			
			a++;
		}
	}
}

while +

import java.util.Scanner;

public class doublewhileplus {
	public static void main (String [] args){

		int a = 1;
		while (a<=3) {
			int b = 1; // 작은 while문의 초기값(반복)을 큰 반복문 안 쪽에 적용.
			while(b<=4) {
				System.out.println(a + " + " + b);
				b++;
			}
			a++;
		}
	}
}

// 출력값 :
1 + 1 =
1 + 2 =
1 + 3 =
1 + 4 =
...
2 + 1 =
...
3 + 4 =

이중 do while문

import java.util.Scanner;

public class doubledowhile {
	public static void main (String [] args){
    
		int a = 1;
		
		do {
			int b = 1; // 작은 dowhile문의 초기값(반복)을 큰 반복문 안쪽에 적용. 
			do {
					System.out.println(a + " + " + b + " = " );
				b++;
			} while (b<=4);
			a++;
		}while(a<=3);
	}
}

do while +

import java.util.Scanner;

public class doubledowhileplus {
	public static void main (String [] args){
    
		int a = 1;
		
		do {
			int b = 1;
			do {
					System.out.println(a + " + " + b + " = " );
				b++;
			} while (b<=4);
			a++;
		}while(a<=3);
	}
}

// 출력값 :
1 + 1 =
1 + 2 =
1 + 3 =
1 + 4 =
...
2 + 1 =
...
3 + 4 =

 

[응용문제] 

5 + 2 = 7

5 + 3 = 8

5 + 4 = 9

5 + 5 = 10

6 + 2 = 8

...

9 + 5 = 14

 

1. for문으로 출력하시오.

public class doublefor {
	public static void main (String [] args){
    
		int a;
        	int b;
        
        	for (a = 5; a <= 9; a++){
        		for (b = 2; b <= 5; b++){
            			System.out.println (a + " + " + b + " = ");
    			}
		}
	}
}

2. while문으로 출력하시오.

public class doublewhile {
	public static void main (String [] args){
    
		int a = 5;
		while(a <= 9) {
			int b = 2;
			while(b <= 5) {
				System.out.println(a + " + " + b + " = ");
				b++;
			}
			a++;
		}
    }
}

3. do while문으로 출력하시오.

public class doublewhile {
	public static void main (String [] args){
    
		int a = 5;
		do {
			int b = 2;
			do {
				System.out.println(a + " + " + b + " = ");
				b++;
			}while(b <= 5);
			a++;
		}while(a <= 9);
    }
}

 

[응용문제]

2 * 1

...

3 * 9

큰 반복문 for 작은 반복문 dowhile 으로 작성.

public class fordowhile {
	public static void main (String [] args){
    
		int a;
		for (a=2; a<=3; a++) {
			int b = 1;
			do {
				System.out.println(a + " * " + b + " = ");
				b++;
			}while(b<=9);
		}
    }
}

[응용문제]

구구단 7단 ~ 9단

단, 각 구구단은 5까지만 ex) 7 * 5 ... 8 * 5 ... 9 * 5

큰 반복문 do while 작은 반복문 while 으로 작성.

public class dowhilewhile {
	public static void main (String [] args){
    
		int a = 7;
		do {
			int b = 1;
			while(b<=5) {
				System.out.println(a + "+" + b + "= ");
				b++;
			}
			a++;
		}while(a<=9);
    }
}

[응용문제]

다음 결과값을 보고 코드를 작성

큰 반복문 for 작은 반복문 while 로 작성.

1 * 1

2 * 2

...

9 * 9

public class forwhile {
	public static void main (String [] args){
    
		int a;
		for (a=1; a<=9; a++) {
			int b = a;
			while(b<=a) {
				System.out.println(a + " * " + b);
				b++;
			}
		}
    }
}

 

[응용문제]

다음 결과값을 보고 코드를 작성

큰 반복문 while 작은 반복문 dowhile 로 작성.

 

1 + 1 = 2

2 + 1 = 3

2 + 2 = 4

3 + 1 = 4

3 + 2 = 5

3 + 3 = 6

4 + 1 = 5

4 + 2 = 6

4 + 3 = 7

4 + 4 = 8

public class whiledowhile {
	public static void main (String [] args){
    

		int a = 1;
		while (a <= 4) {
			int b = 1;
			int c = a + b;
			do {
				System.out.println(a+" + "+ b  +" = " + c );
				b++;
			}while(b<=a);
				a++;
		}
    }
}

 

반복문 합계

 

1 부터 10까지 모든 숫자를 합한 결과값 출력.

 

public class forplus {
	public static void main (String [] args){
    
		int a;
		int total = 0;
		
		for (a = 1; a <= 10; a++) {
			total += a; // == total = total + a;
		}
		System.out.println(total); // 최종 결과값만 출력시, 반복문 종료 후 출력.
    }
}

구구단 2단 2 * 1 부터 2 * 9 까지의 총 합계를 출력하시오.

출력값 = 90

public class maniplus {
	public static void main (String [] args){
    

		int a;
		int b = 2;
		int c;
		int total = 0;
		
		for (a = 1; a <= 9; a++) {
			c = a * b;
			total += c;
		}
		System.out.println(total);
    }
}
반응형

'Back > JAVA' 카테고리의 다른 글

JAVA Step5 (스위치)  (0) 2022.06.06
JAVA Step4-3 (반복문 스캐너)  (0) 2022.06.06
JAVA Step4-1 (반복문)  (0) 2022.06.05
JAVA Step3 (스캐너)  (0) 2022.06.04
JAVA Step2 (반복문)  (0) 2022.06.04
    'Back/JAVA' 카테고리의 다른 글
    • JAVA Step5 (스위치)
    • JAVA Step4-3 (반복문 스캐너)
    • JAVA Step4-1 (반복문)
    • JAVA Step3 (스캐너)
    NOOWGNAJ
    NOOWGNAJ
    Innovation. Development. Evolution. Passion. Smart. Teamwork.

    티스토리툴바