ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 쉽게 배우는 자바 프로그래밍 2판 8장 프로그래밍 문제 Part.2 (6번 ~ 8번)
    JAVA/연습문제 2022. 10. 25. 20:45
    반응형

    # 개발 공부하는 초보자가 작성한 답안이니 정답이 아닐 수 있습니다!!!

     

     

     

    6. 오늘 날짜를 다음 실행 결과 처럼 다양한 방식으로 출력하는 프로그램을 작성하라

    📢주어진 조건📢
    ex) 2022.10.25 화요일 오후 19시 56분 40초(금일 날짜)
    2022.10.15 오후 19:56:40
    오늘은 6월의 15번째 날
    오늘은 2017년의 166번째 날
    🧤정답🧤
    public class Day6Number {
    	public static void main(String[] args) {
    		LocalDateTime ldt = LocalDateTime.now();
    		DateTimeFormatter fm1 = DateTimeFormatter.ofPattern("yyyy.MM.dd a HH:mm:ss");
    		System.out.println(ldt.format(fm1));
    		System.out.println("오늘은 " + ldt.getMonthValue() + "월의 " + ldt.getDayOfMonth() + "번째 날");
    		System.out.println("오늘은 " + ldt.getYear() + "년의 " + ldt.getDayOfYear() + "번째 날");
    	}
    }

     

     

    7. 실행 결과를 보고 다음 프로그램을 완성하라.

    📢주어진 조건📢
    public class MessageFormatTest {
    	public static void main(String[] args) {
    		Object[][] data = { { "세종대왕", 1, "조선" }, { "오바마", 2, "미국" }, { "징기스칸", 3, "몽고" } };
    	}
    }
    📢주어진 결과값📢
    이름 : 세종대왕	  번호 : 1	   국적 : 조선
    이름 : 오바마	  번호 : 2	   국적 : 미국
    이름 : 징기스칸	  번호 : 3	   국적 : 몽고
    🧤정답🧤
    public class MessageFormatTest {
    	public static void main(String[] args) {
    		Object[][] data = { { "세종대왕", 1, "조선" }, { "오바마", 2, "미국" }, { "징기스칸", 3, "몽고" } };
    
    		Object[] data2 = { "번호 : ", "\t이름 : ", "\t\t국적 : " };
    		String s = "";
    		for (int i = 0; i < data.length; i++) {
    			for (int j = 0; j < data[i].length; j++) { // data[i].length
    //				s = MessageFormat.format("이름 : {0}\t번호 : {1}\t\t국적 : {2}", data[i][j], data[i][j + 1], data[i][j + 2]);
    				s = MessageFormat.format("{0}{1}", data2[j], data[i][j]);
    				System.out.print(s);
    			}
    			System.out.println();
    		}
    		// 좀 더 좋다고 생각되는 방식
    		MessageFormat message = new MessageFormat("번호 : {1}\t이름 : {0}\t국적 : {2}");
    		for (int i = 0; i < data.length; i++) {
    			System.out.println(message.format(data[i]));
    
    		}
    	}
    }

     

     

    8. 다음 실행 결과처럼 주어진 영어 속담을 단어로 분리해 배열에 저장하고, 단어의 개수를 출력하며, 정렬한 후 모든 단어를 출력하는 프로그램을 작성하라.

    📢주어진 조건📢
    정렬한 토큰을 저장하는 배열의 크기를 토큰의 개수만큼 생성해야 한다.
    📢주어진 결과값📢
    입력 : Empty vessels make the most sound.
    단어 개수 : 6
    정렬된 토큰 : Empty, make, most, sound., the, vessels,
    🧤정답🧤
    public class WordTest {
    	public static void main(String[] args) {
    		String s = "Empty vessels make the most sound.";
    
    		// StringTokenizer은 기본 공백이 포함이기 때문에 바꿔준다.
            StringTokenizer st = new StringTokenizer(s);  // 같은 말 StringTokenizer st = new StringTokenizer(s, " ");
    		System.out.println(s);
    		System.out.println("단어 개수 : " + st.countTokens());
    
    		String[] s2 = new String[st.countTokens()];
    		System.out.print("정렬된 토큰 : ");
    		for (int i = 0; i < s2.length; i++) {
    			s2[i] = st.nextToken();
    		}
    		Arrays.sort(s2);
    		int number = 0;
    		while (number < s2.length) {
    			System.out.print(s2[number] + ", ");
    			number++;
    		}
    	}
    }

     

     

    반응형

    댓글

Designed by Tistory.