eterno

JAVA 14일차. 복습 및 문제풀어보기 본문

JAVA/강의노트

JAVA 14일차. 복습 및 문제풀어보기

영원한별똥별 2022. 8. 21. 10:31
728x90
반응형

switch문 복습 예제)

 * case문장마다 break로 닫아주지 않으면 조건에 맞는 결과 아래의 문장이 전부 실행된다.

 

for문 복습 예제)


!!배열을 쪼개서 사용할 줄 알아야한다!!

 

문제1) 알파벳 대문자 26개, 알파벳 소문자 26개, 숫자 0 1 2 3 4 5 6 7 8 9, 연산자 + - * /, 특수기호 ! @ # $ % ^ & * ( ) ~ 를 각각 함수로 만들어서 콘솔에 문자 및 16진수를 출력하시오.

 

방법1)

package a.b.c.ch4;

public class Test_CharToHex {

	// 대문자 출력
	public static void alMethod(String aL) {
		System.out.println("Test_CharToHex.alMethod() 함수 시작 >>> : ");
		System.out.println("aL >>> : " + aL);
		
		if (aL != null && aL.length() > 0) {
			for (int i=0; i< aL.length(); i++) {
				char alC = aL.charAt(i);
				System.out.print(" " + alC + " : ");
				System.out.println("0x" + Integer.toHexString(alC));
			}
		}
		System.out.println("Test_CharToHex.alMethod() 함수 종료 >>> : ");			
		System.out.println("=======================================");			
	}
	
	// 소문자 출력
	public static void aSMethod(String aS) {
		System.out.println("Test_CharToHex.aSMethod() 함수 시작 >>> : ");
		System.out.println("aS >>> : " +aS);
		
		if(aS != null && aS.length() >0 ) {
			for (int i=0; i<aS.length(); i++) {
				char alS = aS.charAt(i);
				System.out.print(" " + alS + " : ");
				System.out.println("0x" + Integer.toHexString(alS));
			}
		}
		System.out.println("Test_CharToHex.aSMethod() 함수 종료 >>> : ");	
		System.out.println("=======================================");
	}
	
	// 숫자 출력
	public static void numMethod(String num) {
		System.out.println("Test_CharToHex.numMethod() 함수 시작 >>> : ");
		System.out.println("num >>> : " + num);
		
		if (num!=null && num.length()>0) {
			for (int i=0; i<num.length(); i++ ) {
			char numC = num.charAt(i);
			System.out.print(" " + numC + " : ");
			System.out.println("0x" + Integer.toHexString(numC));
			
			}
		} 
		System.out.println("Test_CharToHex.numMethod() 함수 종료 >>> : ");	
		System.out.println("=======================================");
	}
		
		
		// 사칙연산자 출력
	public static void operMethod(String oper) {
		System.out.println("Test_CharToHex.operMethod() 함수 시작 >>> : ");
		if(oper != null && oper.length()>0) {
			for (int i=0; i<oper.length(); i++) {
				char operA = oper.charAt(i);
				System.out.print(" " + operA + " : ");
				System.out.println("0x" + Integer.toHexString(operA));
			}
		}
		
		System.out.println("Test_CharToHex.operMethod() 함수 종료 >>> : ");	
		System.out.println("=======================================");
	}
		
		// 특수기호 출력
	public static void ksxMethod(String ksx) {
		System.out.println("Test_CharToHex.ksxMethod() 함수 시작 >>> : ");
		if(ksx != null && ksx.length()>0) {
			for (int i=0; i<ksx.length(); i++) {
				char ksxX = ksx.charAt(i);
				System.out.print(" " + ksxX + " : ");
				System.out.println("0x" + Integer.toHexString(ksxX));
			}		
		}
		System.out.println("Test_CharToHex.ksxMethod() 함수 종료 >>> : ");			
	}
	
	//main함수 시작
	public static void main(String[] args) {				
		System.out.println("Test_CharToHex.main() 함수 시작 >>> : ");

		//변수선언
		String aL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String aS = aL.toLowerCase();
		String num = "0123456789";
		String oper = "+-*/";
		String ksx = "!@#$%^&*()~";
		
		Test_CharToHex.alMethod(aL);
		Test_CharToHex.aSMethod(aS);
		Test_CharToHex.numMethod(num);
		Test_CharToHex.operMethod(oper);
		Test_CharToHex.ksxMethod(ksx);
		
		System.out.println("Test_CharToHex.main() 함수 끝 >>> : ");		
			} 
}

 

방법2) 방법1에서 중복되는 구역(프린터역할)을 함수로 묶고 scanner로 입력값을 받는다.

package a.b.c.ch4;

import java.util.Scanner;

public class Test_CharToHex_1 {
	
	// 영문자 대문자
	public static void alMethod(String aL) {
		System.out.println(" Test_CharToHex_1.alMethod() 함수 시작 >>> : ");
		System.out.println(" aL >>> : " + aL);
		
		if (aL !=null && aL.length() > 0) {
			Test_CharToHex_1.toHex_print(aL);	
		}
			
		System.out.println("  Test_CharToHex_1.alMethod() 함수 끝 >>> : ");
	}
	
	// 영문자 소문자
	public static void aSMethod(String aS) {
		System.out.println(" Test_CharToHex_1.aSMethod() 함수 시작 >>> : ");
		System.out.println(" aL >>> : " + aS);
		
		if (aS !=null && aS.length() > 0) {
			Test_CharToHex_1.toHex_print(aS);
		}
			
		System.out.println("  Test_CharToHex_1.aSMethod() 함수 끝 >>> : ");
	}	
	
	// 숫자
	public static void numMethod(String num) {
		System.out.println(" Test_CharToHex_1.numMethod() 함수 시작 >>> : ");
		System.out.println(" num >>> : " + num);
		
		if (num !=null && num.length() > 0) {
			Test_CharToHex_1.toHex_print(num);
		}
			
		System.out.println("  Test_CharToHex_1.numMethod() 함수 끝 >>> : ");
	}
			
	// 사칙 연산자
	public static void operMethod(String oper) {
		System.out.println(" Test_CharToHex_1.oper() 함수 시작 >>> : ");
		System.out.println(" aL >>> : " + oper);
		
		if (oper !=null && oper.length() > 0) {
			Test_CharToHex_1.toHex_print(oper);
		}
			
		System.out.println("  Test_CharToHex_1.oper() 함수 끝 >>> : ");
	}
			
	// 특수기호
	public static void ksxMethod(String ksx) {
		System.out.println(" Test_CharToHex_1.ksxMethod() 함수 시작 >>> : ");
		System.out.println(" ksx >>> : " + ksx);
		
		if (ksx !=null && ksx.length() > 0) {
			Test_CharToHex_1.toHex_print(ksx);
		}
			
		System.out.println("  Test_CharToHex_1.ksxMethod() 함수 끝 >>> : ");
	}
	
	//프린터역할
	public static void toHex_print(String h) {
		for (int i=0; i<h.length(); i++) {
		char hHex = h.charAt(i);
		System.out.print(" " + hHex + " : ");
		System.out.println("0x" + Integer.toHexString(hHex));
		}
	}
	
	public static void main(String[] args) {
		System.out.println("Test_CharToHex_1.main() 함수 시작 >>> : ");
					
		// 변수 선언 
		String aL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String aS = aL.toLowerCase();
		String num = "0123456789";
		String oper = "+-*/";
		String ksx = "!@#$%^&*()~";
		
		System.out.println("ASCII CODE 보기 >>> : ");
		System.out.println("1 : 영문자 대문자 ");
		System.out.println("2 : 영문자 소문자 ");
		System.out.println("3 : 숫자");
		System.out.println("4 : 사칙연산자");
		System.out.println("5 : 특수기호 !@#$%^&*()~");
		
		Scanner sc = new Scanner(System.in);
		int sNum = sc.nextInt();
		
		switch (sNum) { 
		case 1 : Test_CharToHex_1.alMethod(aL);
			break;
		case 2 : Test_CharToHex_1.aSMethod(aS);
			break;
		case 3 : Test_CharToHex_1.numMethod(num);
			break;
		case 4 : Test_CharToHex_1.operMethod(oper);
			break;
		case 5 : Test_CharToHex_1.ksxMethod(ksx);
			break;
		default : 
			System.out.println("1~5중 하나를 선택 하세요!");
		} //end of switch
				
		System.out.println("Test_CharToHex_1.main() 함수 끝 >>> : ");
		sc.close();
	}
																																
}

 

방법3) 방법2에서 scanner를 함수로 만들고 main에서는 호출하는 방법으로 쓸 수도 있다.

package a.b.c.ch4;

import java.util.Scanner;

public class Test_CharToHex_2 {
	
	// scanner
	public void testM(String aL, String aS, String num, String oper, String ksx) {
		
		System.out.println("ASCII CODE 보기 >>> : ");
		System.out.println("1 : 영문자 대문자 ");
		System.out.println("2 : 영문자 소문자 ");
		System.out.println("3 : 숫자");
		System.out.println("4 : 사칙연산자");
		System.out.println("5 : 특수기호 !@#$%^&*()~");
		
		Scanner sc = new Scanner(System.in);
		int sNum = 0;
		
		switch (sNum) { 
		case 1 : Test_CharToHex_2.alMethod(aL);
			break;
		case 2 : Test_CharToHex_2.aSMethod(aS);
			break;
		case 3 : Test_CharToHex_2.numMethod(num);
			break;
		case 4 : Test_CharToHex_2.operMethod(oper);
			break;
		case 5 : Test_CharToHex_2.ksxMethod(ksx);
			break;
		default : 
			System.out.println("1~5중 하나를 선택 하세요!");
		} //end of switch
				
	
		sc.close();
	}
	
	
	// 영문자 대문자
	public static void alMethod(String aL) {
		System.out.println(" Test_CharToHex_1.alMethod() 함수 시작 >>> : ");
		System.out.println(" aL >>> : " + aL);
		
		if (aL !=null && aL.length() > 0) {
			Test_CharToHex_1.toHex_print(aL);	
		}
			
		System.out.println("  Test_CharToHex_1.alMethod() 함수 끝 >>> : ");
	}
	
	// 영문자 소문자
	public static void aSMethod(String aS) {
		System.out.println(" Test_CharToHex_1.aSMethod() 함수 시작 >>> : ");
		System.out.println(" aL >>> : " + aS);
		
		if (aS !=null && aS.length() > 0) {
			Test_CharToHex_1.toHex_print(aS);
		}
			
		System.out.println("  Test_CharToHex_1.aSMethod() 함수 끝 >>> : ");
	}	
	
	// 숫자
	public static void numMethod(String num) {
		System.out.println(" Test_CharToHex_1.numMethod() 함수 시작 >>> : ");
		System.out.println(" num >>> : " + num);
		
		if (num !=null && num.length() > 0) {
			Test_CharToHex_1.toHex_print(num);
		}
			
		System.out.println("  Test_CharToHex_1.numMethod() 함수 끝 >>> : ");
	}
			
	// 사칙 연산자
	public static void operMethod(String oper) {
		System.out.println(" Test_CharToHex_1.oper() 함수 시작 >>> : ");
		System.out.println(" aL >>> : " + oper);
		
		if (oper !=null && oper.length() > 0) {
			Test_CharToHex_1.toHex_print(oper);
		}
			
		System.out.println("  Test_CharToHex_1.oper() 함수 끝 >>> : ");
	}
			
	// 특수기호
	public static void ksxMethod(String ksx) {
		System.out.println(" Test_CharToHex_1.ksxMethod() 함수 시작 >>> : ");
		System.out.println(" ksx >>> : " + ksx);
		
		if (ksx !=null && ksx.length() > 0) {
			Test_CharToHex_1.toHex_print(ksx);
		}
			
		System.out.println("  Test_CharToHex_1.ksxMethod() 함수 끝 >>> : ");
	}
	
		
	//프린터역할
	public static void toHex_print(String h) {
		for (int i=0; i<h.length(); i++) {
		char hHex = h.charAt(i);
		System.out.print(" " + hHex + " : ");
		System.out.println("0x" + Integer.toHexString(hHex));
		}
	}
	
	public static void main(String[] args) {
		System.out.println("Test_CharToHex_1.main() 함수 시작 >>> : ");
					
		// 변수 선언 
		String aL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String aS = aL.toLowerCase();
		String num = "0123456789";
		String oper = "+-*/";
		String ksx = "!@#$%^&*()~";
	
		//Scanner함수를 불러온다.
		Test_CharToHex_2 tc_2 = new Test_CharToHex_2();
		tc_2.testM(aL, aS, num, oper, ksx);
				
		System.out.println("Test_CharToHex_1.main() 함수 끝 >>> : ");
		
	}
																																
}

 

예제) 나머지를 사용하여 값 개수세기

 

문제2) sVar 문자열에 있는 문자 중 D의 개수를 출력하시오.

  • 풀이방법
    • 자바에서 문자열은 문자의 배열이다.
    • 배열은 개수가 있다.
    • 자바에서 문자열의 길이 구하는 함수 : length()함수
    • 자바에서 문자열에서 문자를 추출하는 함수 : charAt(int index)
    • 문자는 ==(상등연산자)로 비교할 수 있다
    • 문자 비교를 ‘D’’로 할지 십진수로할지 16진수로 할지 결정하기 (16진수로 바꾸기)
    • 문자가 같으면 카운트 증가 ++(증감연산자)로 한다.

 

문제3) 짝수 even, 홀수 odd의 개수를 출력하시오.

 ** 내가쓰는 변수를 사용하고 참조할 줄 알아야한다!!

  **변수할당을 어떻게 할지

728x90
반응형