본문 바로가기
STUDY/ㅡㅡ^

아 진짜 짱나는 문제 scanner-중첩for

by eungSe__ 2024. 5. 3.
package oop;

import java.util.Scanner;

/*
 응용문제
 
 사용자가 3개의 값을 무봅니다.
 - 첫번째 숫자 : 2 / 두번째 숫자 : 4 / 짝수 또는 홀수를 입력하세요 : 홀수
 [결과값] 
 3*1 = 3
 3*3 = 9
 3*5 = 15
 3*7 = 21
 3*9 = 27
 
 - 첫번째 숫자 : 2 / 두번째 숫자 : 4 / 짝수 또는 홀수를 입력하세요 : 짝수
 [결과값] 
 2*1 = 2
 ...
 2*9 = 18
 
 3*2 = 6
 3*4 = 12
 3*6 = 18
 3*8 = 24
 
 4*1=4
 ..
 4*9=36
 
 */


public class oop22 {

	public static void main(String[] args) {
		oop22_box oop22 = new oop22_box();
		
		Scanner sc = new Scanner(System.in);
		System.out.println("첫번째 입력값? : ");
		int num1 = sc.nextInt();
		System.out.println("두번째 입력값? : ");
		int num2 = sc.nextInt();
		System.out.println("세번째 입력값? : ");
		String str = sc.next();
		sc.close();
		
		oop22.calc(num1, num2, str);

	}

}

class oop22_box {
	public void calc(int a, int b, String c){
		
		int w;
		if(c.equals("홀수")) {
			w=1;
		}else {
			w=0;
		}
		
		int f1,f2;
		int result = 0;
		for(f1=a; f1<=b; f1++) {
			for(f2=1; f2<=9; f2++) {
				result = f1*f2;	
				if(result%2 == w) {		
					System.out.printf("%s * %s = %s%n",f1,f2,f1*f2);
				}
				
			}
		}

	}
}

'STUDY > ㅡㅡ^' 카테고리의 다른 글

java 셤준비 1  (0) 2024.06.05
풀어볼 문제  (0) 2024.05.30
2024-05-24 숙제  (0) 2024.05.24
0509 숙제 - 로또 만들기  (0) 2024.05.09