Java 입문 클래스 3회차 미션
3회차 미션
배운내용,깨달은 점
어려웠던 점, 반성하고 싶은 점/개선할 방법
사실 처음 문제를 봤을 때는 제가 이 문제를 이렇게 고전할 줄 몰랐습니다. 어려웠던 이유는 항상 써왔던 배열과는 많이 달랐던 이유같습니다. 자주 사용하는 파이썬으로 문제를 풀었다면 배열에 자료형을 굳이 사용하지 않아도 되고 딕셔너리를 사용해도 되므로 쉽게 풀었을 것 같습니다. 하지만 Java에서는 자료형을 미리 지정해주어야 하므로 string으로 값을 받고 비교할 때는 double로 비교하여 가장 작은 값을 찾고 소수점 두자리를 고정해주기 위해서는 string값을 넣어 double로 바꾸어서 소수점 두자리가 고정된 값을 string으로 받았습니다. 이런 점이 어려웠던 것 같고 처음에 문제 접근을 할 때 [[13,13.56],[7,12.157],[2,14]] 이런 식으로 받고 배열의 [x][1]의 값을 기준으로 정렬하고 문제 풀이를 하려했는데 이 방식으로는 선수의 기록을 비교를 하지 못할 것 같아 배열 두개로 나누어 하나는 선수의 번호 하나는 선수의 기록으로 사용하여 선수의 기록은 다른 배열에 넣어 1등의 기록을 구하고 1등의 기록을 가진 index를 찾는 방식으로 만들었습니다. 생각과 다르게 오래걸렸고 조금은 이런 문제도 못푸나 싶어 자괴감에도 조금 빠졌던 것 같습니다. 처음에는 조금 구글링에 의존했었는데 구글링을 잠시 멈추고 문제풀이 방법을 먼저 체계화하고 다시 문제를 풀었더니 리스트를 다른 방식으로 사용하게 되어 문제를 풀게 된 것 같습니다. 앞으로는 마냥 구글링을 먼저하지 않고 먼저 문제풀이 방식을 생각하고 접근하는 방식으로 문제를 푸는 것을 익혀야 할 것 같습니다.
미션제출
쪽집게 과외
연습문제 1 구구단 프로그램
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String inputValue="";
int number = 0;
int MAX_NUMBER=9;
while (true){
System.out.println("[구구단] 몇 단을 출력하실건가요?");
inputValue=sc.next();
if(inputValue.equals("exit")){
System.out.println("프로그램을 성공적으로 종료했습니다.");
return;
}else {
System.out.println(inputValue);
number = Integer.valueOf(inputValue);
}
if(number>MAX_NUMBER && number<1){
System.out.println("1단에서 "+MAX_NUMBER+"단까지만 출력이 가능합니다.");
}
else {
for(int i = 1;i<=9;i++){
System.out.println(number+" * "+i+" = "+number*i);
}
}
}
}
}
연습문제 2 ArrayList와 친해지기
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class Main{
public static void main(String[] args) {
List<Integer> aList = new ArrayList<>();
aList=new ArrayList<Integer>(List.of(5,7,10,9,4));
aList.remove(Integer.valueOf(7));
System.out.println(aList.get(1));
Collections.sort(aList,Collections.reverseOrder());
for (int e : aList) {
System.out.println(e);
}
System.out.println("ArrayList 안에 11의 값이 존재하는가? : "+ aList.contains(11));
}
}
연습문제 3
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.List;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<String> studentList = new ArrayList<>();
String studentName = "";
System.out.println("학생의 이름을 입력하고 엔터를 누르세요. (한글로만 입력해야 합니다.)");
System.out.println("학생들을 다 입력했다면, print라고 입력해주세요.");
while(true){
studentName = sc.next();
if(studentName.equals("print")){
break;
} else if (studentName.matches(".*[가-힣]")) {
studentList.add(studentName);
}else {
System.out.println("학생의 이름은 한글로만 입력해야 합니다.");
}
}
System.out.println("[학생 명단(가나다순)]");
Collections.sort(studentList);
for (String e: studentList) {
System.out.println(e);
}
}
}
연습문제 4
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<List<String>> playerList = new ArrayList<>();
for(int i = 0 ; i< 2 ; i++){
playerList.add(new ArrayList<>());
}
int count = 0;
String playerNumber = "";
String playerRecord = "";
while(true){
System.out.println("선수의 번호를 입력하세요.");
playerNumber= sc.next();
if(playerNumber.equals("print")) {
List<Double> doublePlayerRecord = new ArrayList<>();
for (int i = 0 ; i<playerList.get(1).size() ; i++){
doublePlayerRecord.add(Double.parseDouble(playerList.get(1).get(i)));
}
Collections.sort(doublePlayerRecord);
double winnerRecord = doublePlayerRecord.get(0);
int winnerIndex = playerList.get(1).indexOf(String.format("%.2f",winnerRecord));
System.out.println("1등 : " + playerList.get(0).get(winnerIndex) + "번 선수 / " + playerList.get(1).get(winnerIndex) + "초 (참가인원 : " + playerList.get(0).size() + "명)");
break;
} else if (!(playerList.get(0).contains(playerNumber))) {
System.out.println("이 선수의 100m 달리기 기록이 몇 초인지 입력하세요. elif");
playerRecord= sc.next();
playerList.get(0).add(playerNumber);
playerList.get(1).add(String.format("%.2f",Double.parseDouble(playerRecord)));
} else {
System.out.println("이 선수의 100m 달리기 기록이 몇 초인지 입력하세요. else");
playerRecord= sc.next();
int playerIndex = playerList.get(0).indexOf(playerNumber);
double beforePlayerRecord = Double.parseDouble(playerList.get(1).get(playerIndex));
if(Double.parseDouble(playerRecord)<beforePlayerRecord){
playerList.get(1).set(playerIndex, String.format("%.2f",Double.parseDouble(playerRecord)));
}
}
}
}
}
궁금한 점
1. 제가 푼 방식이 멘토님의 문제 출제 의도에 맞았나 궁금합니다.
2. 개선해야 할 점이 있는지 확인하고 싶습니다!
3. 가끔씩 질문하고 싶은 것을 적으려다가도 아 이건 충분히 검색으로 찾을 수 있지 않나? 라는 생각이 들게 됩니다. 제가 성장하기 위해서 멘토님들에게 어떻게 질문하는 것이 좋을지가 궁금합니다..!