[Java] Solution to puzzles

Joy๐ŸŒฑยท2023๋…„ 1์›” 1์ผ
0

๐Ÿงฉ Coding Challenges

๋ชฉ๋ก ๋ณด๊ธฐ
8/20
post-thumbnail

๐Ÿงฉ ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ์— ๋”ฐ๋ผ ๋ฉ”์†Œ๋“œ ์ž‘์„ฑํ•˜๊ธฐ

๐Ÿ’โ€ ์ตœ๋Œ€ 10๋ช…์˜ ํ•™์ƒ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. ํ•ด๋‹น ํ•™์ƒ์˜ ํ•™๋…„, ๋ฐ˜, ์ด๋ฆ„, ๊ตญ์–ด, ์ˆ˜ํ•™, ์˜์–ด ์ ์ˆ˜๋ฅผ ๊ธฐ๋กํ•˜๊ณ  ํ‰๊ท ์„ ๊ตฌํ•ด๋ณด์„ธ์š”. ํ•ด๋‹น ๊ตฌํ˜„ ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ๊ณผ ํด๋ž˜์Šค ๊ตฌ์กฐ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Input & Output

ํ•™๋…„ : 6
๋ฐ˜ : 1
์ด๋ฆ„ : ํšจ์—ฐ
๊ตญ์–ด์ ์ˆ˜ : 100
์˜์–ด์ ์ˆ˜ : 90
์ˆ˜ํ•™์ ์ˆ˜ : 95
๊ณ„์† ์ถ”๊ฐ€ํ•  ๊ฒ๋‹ˆ๊นŒ? (y/n) : y
ํ•™๋…„ : 5
๋ฐ˜ : 3
์ด๋ฆ„ : ์ˆ˜๋ฏผ
๊ตญ์–ด์ ์ˆ˜ : 90
์˜์–ด์ ์ˆ˜ : 100
์ˆ˜ํ•™์ ์ˆ˜ : 85
๊ณ„์† ์ถ”๊ฐ€ํ•  ๊ฒ๋‹ˆ๊นŒ? (y/n) : y
ํ•™๋…„ : 2
๋ฐ˜ : 7
์ด๋ฆ„ : ์น˜์ฆˆ
๊ตญ์–ด์ ์ˆ˜ : 85
์˜์–ด์ ์ˆ˜ : 100
์ˆ˜ํ•™์ ์ˆ˜ : 100
๊ณ„์† ์ถ”๊ฐ€ํ•  ๊ฒ๋‹ˆ๊นŒ? (y/n) : n
ํ•™๋…„ = 6, ๋ฐ˜ = 1, ์ด๋ฆ„ = ํšจ์—ฐ, ๊ตญ์–ด = 100, ์ˆ˜ํ•™ = 95, ํ‰๊ท  = 95
ํ•™๋…„ = 5, ๋ฐ˜ = 3, ์ด๋ฆ„ = ์ˆ˜๋ฏผ, ๊ตญ์–ด = 90, ์ˆ˜ํ•™ = 85, ํ‰๊ท  = 91
ํ•™๋…„ = 2, ๋ฐ˜ = 7, ์ด๋ฆ„ = ์น˜์ฆˆ, ๊ตญ์–ด = 85, ์ˆ˜ํ•™ = 100, ํ‰๊ท  = 95
์„ฑ์ ์ด ๊ธฐ๋ก๋œ ์ด ํ•™์ƒ๋“ค์˜ ์ˆ˜๋Š” 3๋ช… ์ž…๋‹ˆ๋‹ค.

โ—ผ StudentDTO Class

public class StudentDTO {

	/* ํ•„๋“œ */
	private int grade;		// ํ•™๋…„
	private int classroom;	// ๋ฐ˜
	private String name;	// ์ด๋ฆ„
	private int kor;		// ๊ตญ์–ด์ ์ˆ˜
	private int eng;		// ์˜์–ด์ ์ˆ˜
	private int math;		// ์ˆ˜ํ•™์ ์ˆ˜
	
	/* ์ƒ์„ฑ์ž */
	public StudentDTO () {}
	
	public StudentDTO(int grade, int classroom, String name, int kor, int eng, int math) {
		this.grade = grade;
		this.classroom = classroom;
		this.name = name;
		this.kor = kor;
		this.eng = eng;
		this.math = math;
	}
	
	/* ๋ฉ”์†Œ๋“œ */

	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public int getClassroom() {
		return classroom;
	}

	public void setClassroom(int classroom) {
		this.classroom = classroom;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getKor() {
		return kor;
	}

	public void setKor(int kor) {
		this.kor = kor;
	}

	public int getEng() {
		return eng;
	}

	public void setEng(int eng) {
		this.eng = eng;
	}

	public int getMath() {
		return math;
	}

	public void setMath(int math) {
		this.math = math;
	}
	
	/* ์ถœ๋ ฅ ๋ฉ”์†Œ๋“œ */
	public String getInformation() {
		return "ํ•™๋…„ = " + grade + ", ๋ฐ˜ = " + classroom + ", ์ด๋ฆ„ = " + name + ", ๊ตญ์–ด = " + kor + ", ์ˆ˜ํ•™ = " + math + ", ํ‰๊ท  = ";
	}
}

โ—ผ Application Class

public class Application {

	public static void main(String[] args) {
		
		// ํ•„์š”ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ
		Scanner sc = new Scanner(System.in);
		
		// ๊ฐ์ฒด ๋ฐฐ์—ด ํ• ๋‹น
		StudentDTO[] studentsArray = new StudentDTO[10]; 	>>> ์ž…๋ ฅํ•  ํ•™์ƒ ๋ฐฐ์—ด
		int[] avgArray = new int[10]; 						>>> ํ•™์ƒ ์„ฑ์  ํ‰๊ท  ๋ฐฐ์—ด
		
		// ํ•™์ƒ ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›์„ while๋ฌธ
		int studentsCount = 0; >>> ๊ธฐ๋ก๋œ ์ด ํ•™์ƒ ์ˆ˜
		int i = 0;
		
		XXX :
		while(true) {
			System.out.print("ํ•™๋…„ : ");
			int grade = sc.nextInt();
			System.out.print("๋ฐ˜ : ");
			int classroom = sc.nextInt();
			sc.nextLine();
			System.out.print("์ด๋ฆ„ : ");
			String name = sc.nextLine();
			System.out.print("๊ตญ์–ด์ ์ˆ˜ : ");
			int kor = sc.nextInt();
			System.out.print("์˜์–ด์ ์ˆ˜ : ");
			int eng = sc.nextInt();
			System.out.print("์ˆ˜ํ•™์ ์ˆ˜ : ");
			int math = sc.nextInt();
			
			// ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ๋ฐฐ์—ด์— ์ €์žฅ
			studentsArray[i] = new StudentDTO(grade, classroom, name, kor, eng, math);
			avgArray[i] = (kor + eng + math) / 3;
			studentsCount ++;
            i++;
            >>> ์ž…๋ ฅ๋œ ๊ฐ’์ด studentsArray[i]์— ์ €์žฅ๋˜์–ด๊ฐ
			
			// ํ•™์ƒ์„ ๊ณ„์† ์ถ”๊ฐ€ํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฌป๋Š” if๋ฌธ
			System.out.print("๊ณ„์† ์ถ”๊ฐ€ํ•  ๊ฒ๋‹ˆ๊นŒ? (y/n) : ");
			char yesOrNo = sc.next().charAt(0);
			
            if(yesOrNo == 'y'||yesOrNo == 'Y') { 
					continue XXX;	>>> y/Y๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด while๋ฌธ ๋‹ค์‹œ ์‹œ์ž‘
			} else {
				for(int j = 0; j < i; j++) {
						System.out.println(studentsArray[j].getInformation() + avgArray[j]);
				
                }break XXX;			>>> ๋‹ค๋ฅธ๊ฒŒ ์ž…๋ ฅ๋˜๋ฉด ์ถœ๋ ฅ ํ›„ while๋ฌธ ์ข…๋ฃŒ
			} 
		} System.out.println("์„ฑ์ ์ด ๊ธฐ๋ก๋œ ์ด ํ•™์ƒ๋“ค์˜ ์ˆ˜๋Š” " + studentsCount + "๋ช… ์ž…๋‹ˆ๋‹ค.");
	}
}

๐Ÿ’ฌ Overall Comment

* ์ด๋ฒˆ ๋ฌธํ•ญ์—์„œ๋Š” ์ •๋ง ๋ฐฐ์šด ๊ฒƒ์ด ๋งŽ๋‹ค. ๊ต‰์žฅํžˆ ์‹œ๊ฐ„์„ ๋งŽ์ด ์Ÿ์•„๋ถ€์—ˆ์ง€๋งŒ ์ข€์ฒ˜๋Ÿผ ํ’€๋ฆฌ์ง€ ์•Š๋Š” ๋ถ€๋ถ„์ด ๋งŽ์•˜๋‹ค. 
  ์ž…๋ ฅ๋ฐ›์€ ํ•™์ƒ๋“ค์„ ์ €์žฅํ•  ๋•Œ for๋ฌธ์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ, ์ด๋ฏธ while๋ฌธ ์•ˆ์— ์†ํ•ด์žˆ์œผ๋ฏ€๋กœ ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†์—ˆ๋‹ค. 
  ๊ณต๊ฐ„์„ ๋งŒ๋“ค์–ด๋‘” studentsArray์— DTO์˜ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ ์ €์žฅํ•˜๋ฉด ํ•ด๊ฒฐ๋˜๋Š” ๋ถ€๋ถ„์ด์—ˆ๋‹ค. 
  ์ด๊ฒƒ์ด ์›์ธ์ด ๋˜์–ด ์ž…๋ ฅ๋œ ํ•™์ƒ๋“ค์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์Œ“์ด์ง€ ์•Š๊ณ  ๊ณ„์† ๋ฎ์–ด์”Œ์›Œ์ ธ ๋งˆ์ง€๋ง‰์— ์ž…๋ ฅ๋œ ํ•™์ƒ๋งŒ ์ถœ๋ ฅ์ด ๋˜์—ˆ์—ˆ๋‹ค.
* ๋˜ํ•œ, ๊ฐ ํ•™์ƒ๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์—ฌ ๋„ฃ๋Š” ๋ถ€๋ถ„์—์„œ ๋„์ €ํžˆ ์•„์ด๋””์–ด๊ฐ€ ์—†์–ด ์Šคํ„ฐ๋””ํŒ€์›๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๋„ฃ์—ˆ๋Š”๋ฐ, 
  ์ด๊ฒƒ ์—ญ์‹œ ๋ฐฐ์—ด์„ ์™„์„ฑํ•˜๊ณ  studentsArray๊ฐ€ ์ฆ๊ฐ€๋จ๊ณผ ๋™์‹œ์— ํ‰๊ท ์˜ ๊ฐฏ์ˆ˜๋„ ์ฆ๊ฐ€๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ ๋ณ€์ˆ˜ i๋ฅผ ๋„ฃ์œผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. 
* ๊ทธ๋ฆฌ๊ณ  lable์ธ XXX๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ, ์‚ฌ์šฉํ•  ๋•Œ ๊ธฐ์ค€์ ์„ ์—ฌ๋Ÿฌ๊ฐœ ๋‘˜ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๊นจ๋‹ฌ์•˜๋‹ค. 
  ์ •๋ง ๋งŽ์ด ๊ณต๋ถ€ํ•˜๊ณ  ๋˜ ๋ฐฐ์›Œ์•ผ๊ฒ ๋‹ค.

๐Ÿ’ก Another Solution

public class Application {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		StudentDTO[] students = new StudentDTO[10];
		
		int index = 0;
		
		while(index < students.length){
			
			System.out.print("ํ•™๋…„: ");
			int grade = sc.nextInt();
			
			System.out.print("๋ฐ˜: ");
			int classroom = sc.nextInt();
			sc.nextLine();
			
			System.out.print("์ด๋ฆ„ : ");
			String name = sc.nextLine();
			
			System.out.print("๊ตญ์–ด์ ์ˆ˜ : ");
			int kor = sc.nextInt();
			
			System.out.print("์˜์–ด์ ์ˆ˜ : ");
			int eng = sc.nextInt();
			
			System.out.print("์ˆ˜ํ•™์ ์ˆ˜ : ");
			int math = sc.nextInt();
			sc.nextLine();
			
			students[index++] = new StudentDTO(grade, classroom, name, kor, eng, math);
			
			System.out.print("๊ณ„์† ์ถ”๊ฐ€ํ•  ๊ฒ๋‹ˆ๊นŒ? (y/n) : ");
			//char anwser = sc.nextLine().toUpperCase().charAt(0);
			//if(anwser != 'Y') break;
			
			String anwser = sc.nextLine();
			if(!anwser.equalsIgnoreCase("y")) break;
		}
		for(int i = 0; i < index; i++) {
			int sum = students[i].getKor() + students[i].getEng() + students[i].getMath();
			System.out.println(students[i].getInformation() + ", ํ‰๊ท =" + sum/3);
		}
	}
}

profile
Tiny little habits make me

0๊ฐœ์˜ ๋Œ“๊ธ€