반응형

 

문제

 

1. 등수대로 출력하시오.

 

package kr.or.ddit.homework;

import java.util.Scanner;

public class HomeWork8 {
	Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		HomeWork8 obj = new HomeWork8();
		obj.process();
	}
	
	private void process() {
		// 5명의 3과목 성적과 이름을 배열에 저장하고 
		// 총점과 평균과 등수를 구하여 출력 하시오.
		
		String[] name = {"김영훈", "박채연", "최진호", "김미선", "서혜진"};
		int[][] score = { {85,72,81,0,0,1},
					 	  {67,90,87,0,0,1},
					 	  {77,79,94,0,0,1},
					 	  {80,90,52,0,0,1},
					 	  {97,65,77,0,0,1}
						};
		
		/*
		 *   총점 평균 구하기
		 */
//		
//		score[0][3] = score[0][0] + 영어 + 수학;
		for(int i=0; i<score.length; i++) {
			score[i][3] = score[i][0] + score[i][1] + score[i][2];
			score[i][4] = score[i][3]/3;
		}
		// 등수 구하기.
		
		// i = 본인
		for(int i=0; i<score.length; i++) {
			// 다른사람
			for(int j=0; j<score.length; j++) {
				// 내 점수보다 다른 사람 점수가 더 크다면
				// 내 등수를 하나 올린다.
				if(score[i][3] < score[j][3]) {
					score[i][5]++;
				}
			}
		}
        
		// 정렬하기.
		System.out.println("정렬 전");
		System.out.println("이름\t국어\t영어\t수학\t총점\t평균\t등수");
		System.out.println("---------------------------------------------------");
		for(int i=0; i<score.length; i++) {
			System.out.print(name[i]+"\t ");
			for(int j=0; j<score[i].length; j++) {
				System.out.print(score[i][j]+"\t");
			}
			System.out.println();
		}
		
		
		
		System.out.println("정렬 후");
		System.out.println("이름\t국어\t영어\t수학\t총점\t평균\t등수");
		System.out.println("---------------------------------------------------");
		for(int i=0; i<score.length; i++) {
			System.out.print(name[i]+"\t ");
			for(int j=0; j<score[i].length; j++) {
				System.out.print(score[i][j]+"\t");
			}
			System.out.println();
		}
		System.out.println("---------------------------------------------------");
		
	}
}

 

 

2. 단어 맞추기 (행맨)을 완성하시오.

 

package kr.or.ddit.study05.sec02;

import java.util.Scanner;

public class ArrayExample04 {
	Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		ArrayExample04 obj = new ArrayExample04();
		obj.process();
	}
	
	public void process() {
		/*
		 *   단어 맞추기 (행맨)
		 *   apple
		 *   
		 *   랜덤한 단어가 주어지고 단어 숫자를 알려줌. 
		 *   -> ㅇㅇㅇㅇㅇ
		 *   똑같은 글자가 있는 경우
		 *   -> aㅇㅇㅇㅇ
		 */
		 String word = "apple";
		 // 문자열 -> 문자 배열로 바꾸기 
		 char[] question;
		 // 프린트 배열에 글자 수만큼 채우기. -> ㅇㅇㅇㅇㅇ
		 char[] print; 
         while(true) {
        	 /*
        	  *   스캐너를 통해 문자를 입력 
        	  */
        	 
        	 /*
        	  *  입력 받은 문자와 question 비교.
        	  *  같다면 print 값 변경하기.
        	  */
        	 
        	 /*
        	  *  정답을 다 맞춘경우 while문 종료.
        	  */
        	 
         }
	}
	
}

 

 

 

1번 문제

 

package kr.or.ddit.homework;

import java.util.Scanner;

public class HomeWork8 {
	Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		HomeWork8 obj = new HomeWork8();
		obj.process();
	}
	
	private void process() {
		// 5명의 3과목 성적과 이름을 배열에 저장하고 
		// 총점과 평균과 등수를 구하여 출력 하시오.
		
		String[] name = {"김영훈", "박채연", "최진호", "김미선", "서혜진"};
		int[][] score = { {85,72,81,0,0,1},
					 	  {67,90,87,0,0,1},
					 	  {77,79,94,0,0,1},
					 	  {80,90,52,0,0,1},
					 	  {97,65,77,0,0,1}
						};
		
		/*
		 *   총점 평균 구하기
		 */
//		
//		score[0][3] = score[0][0] + 영어 + 수학;
		for(int i=0; i<score.length; i++) {
			score[i][3] = score[i][0] + score[i][1]+score[i][2];
			score[i][4] = score[i][3]/3;
		}
		// 등수 구하기.
		
		// i = 본인
		for(int i=0; i<score.length; i++) {
			// 다른사람
			for(int j=0; j<score.length; j++) {
				// 내 점수보다 다른 사람 점수가 더 크다면
				// 내 등수를 하나 올린다.
				if(score[i][3] < score[j][3]) {
					score[i][5]++;
				}
			}
		}
		
		// 정렬하기.
		System.out.println("정렬 전");
		System.out.println("이름\t국어\t영어\t수학\t총점\t평균\t등수");
		System.out.println("---------------------------------------------------");
		for(int i=0; i<score.length; i++) {
			System.out.print(name[i]+"\t ");
			for(int j=0; j<score[i].length; j++) {
				System.out.print(score[i][j]+"\t");
			}
			System.out.println();
		}
		
		System.out.println("---------------------------------------------------");
//		for(int i=0; i<score.length; i++) {
//			for(int j=0; j<score.length; j++) {
//				if(score[i][5] < score[j][5]) {
//					int[] temp = score[i];
//					score[i] = score[j];
//					score[j] = temp;
//					
//					String temp_s = name[i];
//					name[i] = name[j];
//					name[j] = temp_s;
//				}
//			}
//		}
		
		for(int i=0; i<score.length-1; i++) {
			for(int j=0; j<score.length-1; j++) {
				int sum1 = score[j][3];
				int sum2 = score[j+1][3];
				
				if(sum1 < sum2) {
					int[] temp = score[j];
					score[j] = score[j+1];
					score[j+1] = temp;
				}
			}
		}
		
		System.out.println("정렬 후");
		System.out.println("이름\t국어\t영어\t수학\t총점\t평균\t등수");
		System.out.println("---------------------------------------------------");
		for(int i=0; i<score.length; i++) {
			System.out.print(name[i]+"\t ");
			for(int j=0; j<score[i].length; j++) {
				System.out.print(score[i][j]+"\t");
			}
			System.out.println();
		}
		System.out.println("---------------------------------------------------");
		
	}
}

 

결과 화면1

 

 

 

2번 문제

 

package kr.or.ddit.study05.sec02;

import java.util.Arrays;
import java.util.Scanner;

public class ArrayExample04 {
	Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		ArrayExample04 obj = new ArrayExample04();
		obj.process();
	}

	public void process() {
		/*
		 * 단어 맞추기 (행맨) apple
		 * 
		 * 랜덤한 단어가 주어지고 단어 숫자를 알려줌. -> ㅇㅇㅇㅇㅇ 똑같은 글자가 있는 경우 -> aㅇㅇㅇㅇ
		 */
		String word = "apple";

		// 문자열 -> 문자 배열로 바꾸기
		char[] question = word.toCharArray();

		// 프린트 배열에 글자 수만큼 채우기. -> ㅇㅇㅇㅇㅇ
		char[] print = new char[question.length];
		for (int i = 0; i < question.length; i++) {
			print[i] = '◇';
		}

		while (true) {
			/*
			 * 스캐너를 통해 문자를 입력
			 */
			System.out.println(Arrays.toString(print));
			System.out.println("알파벳을 입력하세요.");
			String answer = sc.next();
			char ch = answer.charAt(0);

			/*
			 * 입력 받은 문자와 question 비교. 같다면 print 값 변경하기.
			 */
			for (int i = 0; i < question.length; i++) {
				if (question[i] == ch) {
					print[i] = ch;
				}
			}

			/*
			 * 정답을 다 맞춘경우 while문 종료.
			 */

			/*
			 * 1. String 하나 선언후 print 값 전체 더하기 word 랑 같으면 정답.
			 */
//        	 String temp = "";

			// temp에 print 값 전체 더하기.
//        	 for(int i=0; i<print.length; i++) {
//        		 temp += print[i];
//        	 }
//        	 
//        	 // word랑 temp 같으면 break;
//        	 if( temp.equals(word) ) {
//        		 System.out.println("\n" + Arrays.toString(print));
//        		 break;
//        	 }

			/*
			 * 2. print 배열과 question 배열 전체가 같으면 정답.
			 */

			// for 문
			int cnt = 0;
			for (int i = 0; i < question.length; i++) {
				// print 배열과 question 배열이 같은지 비교.
				if (question[i] == print[i]) {
					cnt++;
				}
			}
			if (cnt == question.length) {
				System.out.println("\n" + Arrays.toString(print));
				break;
			}

			// 또는 아래와 같은 방법을 사용할 수도 있음
//        	 boolean end = true;
//        	 for(int i=0; i<question.length; i++) {
//        		if(question[i] != print[i]) {
//        			end = false;
//        		}
//        	 }
//        	 if(end) break;

		}
		System.out.println("정답입니다.");
	}
}

 

결과 화면2

 

 

반응형

'자바' 카테고리의 다른 글

[Java 초급] 13.5.1장 테스트1  (0) 2023.12.18
[Java 초급] 13장 클래스  (0) 2023.12.15
[Java 초급] 12장 배열 복사  (0) 2023.12.13
[Java 초급] 10.5.2장 테스트  (0) 2023.12.12
[Java 초급] 11장 2차원 배열  (0) 2023.12.12