다라다라V
article thumbnail
728x90
반응형

💡 

 

[정답]

import java.util.Scanner;
import java.util.Vector;

public class DaraSolution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Vector<Integer> v = new Vector<>();
        System.out.println("정수(-1이 입력될 때까지)>> ");

        // 입력받은 정수를 벡터에 저장하고 가장 큰 수를 찾아 출력
        int max = 0;
        while (true) {
            int num = scanner.nextInt();
            if (num == -1) {
                break;
            }
            v.add(num);
            if (num > max) {
                max = num;
            }
        }
        System.out.println("가장 큰 수는 " + max);
    }
}

 

💡 

 

[정답]

import java.util.ArrayList;
import java.util.Scanner;

public class DaraSolution {
    public static void main(String[] args) {
        // 학점을 저장할 리스트 생성
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> list = new ArrayList<>();
        double sum = 0.0;

        // 6개의 학점을 입력받아 리스트에 저장
        System.out.print("6개의 학점을 빈 칸으로 분리 입력(A/B/C/D/F)>> ");
        for (int i = 0; i < 6; i++) {
            list.add(scanner.next());
        }

        // 리스트에 저장된 학점을 순회하며 학점에 따라 학점을 계산
        for (String grade : list) {
            switch (grade) {
                case "A":
                    sum += 4.0;
                    break;
                case "B":
                    sum += 3.0;
                    break;
                case "C":
                    sum += 2.0;
                    break;
                case "D":
                    sum += 1.0;
                    break;
                case "F":
                    sum += 0.0;
                    break;
            }
        }

        System.out.println(sum / 6.0);
        scanner.close();
    }
}

 

💡 

 

[정답]

import java.util.ArrayList;
import java.util.Scanner;

public class DaraSolution {
    public static void main(String[] args) {
        ArrayList<String> countries = new ArrayList<>();
        ArrayList<Integer> populations = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);

        // 나라 이름과 인구를 입력받아 저장 (띄어쓰기로 구분)
        System.out.println("나라 이름과 인구를 입력하세요.(예: Korea 5000)");
        while (true) {
            System.out.print("나라 이름, 인구 >> ");
            String country = scanner.next();
            if (country.equals("그만")) {
                break;
            }
            int population = scanner.nextInt();
            countries.add(country);
            populations.add(population);
        }

        // 나라 이름을 입력받아 인구 출력
        while (true) {
            System.out.print("인구 검색 >> ");
            String country = new java.util.Scanner(System.in).next();
            if (country.equals("그만")) {
                break;
            }
            int index = countries.indexOf(country);
            if (index == -1) {
                System.out.println("나라 이름이 없습니다.");
            } else {
                System.out.println(country + "의 인구는 " + populations.get(index));
            }
        }
    }
}

 

💡 

 

[정답]

import java.util.Scanner;
import java.util.Vector;

public class DaraSolution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Vector<Integer> v = new Vector<Integer>();
        int sum = 0;
        int count = 0;

        while (true) {
            System.out.println("강수량 입력 (0 입력시 종료)>> ");
            int n = scanner.nextInt();
            if (n == 0) {
                break;
            }
            v.add(n);

            // 벡터에 저장된 값 출력
            for (int i = 0; i < v.size(); i++) {
                System.out.print(v.get(i) + " ");
            }
            System.out.println();
            
            // 평균 계산
            sum += n;
            count++;
            System.out.println("현재 평균 " + sum / count);
        }
    }
}

 

💡 

 

[정답]

(1)

import java.util.Scanner;
import java.util.ArrayList;

class Student {
    // 이름, 학과, 학번, 학점평균
    private String name;
    private String dept;
    private String num;
    private double grade;

    public Student(String name, String dept, String num, double grade) {
        this.name = name;
        this.dept = dept;
        this.num = num;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public String getDept() {
        return dept;
    }

    public String getNum() {
        return num;
    }

    public double getGrade() {
        return grade;
    }
}
public class DaraSolution {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<Student>();
        Scanner scanner = new Scanner(System.in);

        // 입력받은 정보를 기반으로 학생 객체 4개 생성
        System.out.println("학생 이름, 학과, 학번, 학점평균을 입력하세요.");
        for (int i = 0; i < 4; i++) {
            System.out.print(">> ");
            String name = scanner.next();
            String dept = scanner.next();
            String num = scanner.next();
            double grade = scanner.nextDouble();
            list.add(new Student(name, dept, num, grade));
        }

        // 학생 정보 출력
        for (Student s : list) {
            System.out.println("--------------------");
            System.out.println("이름: " + s.getName());
            System.out.println("학과: " + s.getDept());
            System.out.println("학번: " + s.getNum());
            System.out.println("학점평균: " + s.getGrade());
        }

        // 학생 이름을 입력받고 학생 정보 출력
        while (true) {
            System.out.print("학생 이름 >> ");
            String name = scanner.next();
            if (name.equals("그만")) {
                break;
            }
            boolean isExist = false;
            for (Student s : list) {
                if (name.equals(s.getName())) {
                    System.out.println(s.getName() + ", " + s.getDept() + ", " 
                    	+ s.getNum() + ", " + s.getGrade());
                }
            }
            if (!isExist) {
                System.out.println("해당 학생이 존재하지 않습니다.");
            }
        }
    }
}

 

(2)

import java.util.HashMap;
import java.util.Scanner;
import java.util.ArrayList;

class Student {
    // 이름, 학과, 학번, 학점평균
    private String name;
    private String dept;
    private String num;
    private double grade;

    public Student(String name, String dept, String num, double grade) {
        this.name = name;
        this.dept = dept;
        this.num = num;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public String getDept() {
        return dept;
    }

    public String getNum() {
        return num;
    }

    public double getGrade() {
        return grade;
    }
}
public class DaraSolution {
    public static void main(String[] args) {
        HashMap<String, Student> map = new HashMap<>();
        Scanner scanner = new Scanner(System.in);
        
        // 학생 정보 입력받기
        System.out.println("학생 이름, 학과, 학번, 학점평균 입력하세요. ");
        while (true) {
            System.out.print(">> ");
            String name = scanner.next();
            if (name.equals("그만")) {
                break;
            }
            String dept = scanner.next();
            String num = scanner.next();
            double grade = scanner.nextDouble();
            map.put(num, new Student(name, dept, num, grade));
        }
        
        // 학생 정보 검색
        while (true) {
            System.out.print("학생 학번 >> ");
            String num = scanner.next();
            if (num.equals("그만")) {
                break;
            }
            Student student = map.get(num);
            if (student == null) {
                System.out.println(num + " 학번 학생은 없습니다.");
            } else {
                System.out.println(student.getName() + ", " + student.getDept() 
                        + ", " + student.getNum() + ", " + student.getGrade());
            }
        }
    }
}

💡 

 

[정답]

import java.util.HashMap;
import java.util.Scanner;

class Location {
    private String cityName;
    private double latitude;
    private double longitude;

    public Location(String cityName, double latitude, double longitude) {
        this.cityName = cityName;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public String getCityName() {
        return cityName;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }
}

public class DaraSolution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        HashMap<String, Location> map = new HashMap<>();

        System.out.println("도시, 위도, 경도를 입력하세요.");
        // , 를 기준으로 삼아 나눔
        for (int i = 0; i < 4; i++) {
            System.out.print(">> ");
            String[] input = scanner.nextLine().split(", ");
            String cityName = input[0];
            double latitude = Double.parseDouble(input[1]);
            double longitude = Double.parseDouble(input[2]);
            map.put(cityName, new Location(cityName, latitude, longitude));
        }

        // 해시맵에 있는 모든 도시 출력
        System.out.println("--------------------");
        for (String key : map.keySet()) {
            Location location = map.get(key);
            System.out.println(location.getCityName() + " " + location.getLatitude() 
            	+ " " + location.getLongitude());
        }

        // 도시 이름으로 검색
        System.out.println("--------------------");
        while (true) {
            System.out.print("도시 이름 >> ");
            String cityName = scanner.nextLine();
            if (cityName.equals("그만")) {
                break;
            }
            Location location = map.get(cityName);
            if (location == null) {
                System.out.println(cityName + "는 없는 도시입니다.");
            } else {
                System.out.println(location.getCityName() + " " + location.getLatitude() 
                	+ " " + location.getLongitude());
            }
        }
    }
}

 

💡 

 

[정답]

import java.util.HashMap;
import java.util.Scanner;


public class DaraSolution {
    public static void main(String[] args) {
        System.out.println("** 포인트 관리 프로그램입니다. **");
        Scanner scanner = new Scanner(System.in);
        HashMap<String, Integer> map = new HashMap<>();

        while (true) {
            System.out.print("이름과 포인트 입력>> ");
            String input = scanner.nextLine();

            // 그만을 입력하면 종료
            if (input.equals("그만")) {
                break;
            }

            // 이름과 포인트를 입력받아 해시맵에 저장
            String[] arr = input.split(" ");
            String name = arr[0];
            int point = Integer.parseInt(arr[1]);
            if (map.containsKey(name)) {
                map.put(name, map.get(name) + point);
            } else {
                map.put(name, point);
            }

            // 모든 고객의 정보 출력
            for (String key : map.keySet()) {
                System.out.print("(" + key + "," + map.get(key) + ")");
            }
        }
    }
}

 

💡 

 

[정답]

interface IStack<T> {
    T pop();
    boolean push(T ob);
}

class MyStack<T> implements IStack<T> {
    private int tos;
    private T[] data;

    public MyStack(int size) {
        tos = 0;
        data = (T[]) new Object[size];
    }

    @Override
    public T pop() {
        if (tos == 0) {
            return null;
        }
        tos--;
        return data[tos];
    }

    @Override
    public boolean push(T ob) {
        if (tos == data.length) {
            return false;
        }
        data[tos] = ob;
        tos++;
        return true;
    }
}

public class StackManager {
    public static void main(String[] args) {
        IStack<Integer> stack = new MyStack<>(10);

        for (int i = 0; i < 10; i++) {
            stack.push(i);
        }
        // 스택이 빌 때까지 pop
        while (true) {
            Integer n = stack.pop();
            if (n == null) {
                break;
            }
            System.out.print(n + " ");
        }
    }
}

 

💡 

 

[정답]

import java.util.Scanner;
import java.util.Vector;

public class GraphicEditor {
    private Vector<Shape> shapes = new Vector<>();
    private Scanner scanner = new Scanner(System.in);

    public void insert() {
        System.out.println("Line(1), Rect(2), Circle(3) >> ");
        int choice = scanner.nextInt();
        scanner.nextLine(); // consume the leftover newline

        Shape shape = null;
        switch (choice) {
            case 1:
                shape = new Line();
                break;
            case 2:
                shape = new Rect();
                break;
            case 3:
                shape = new Circle();
                break;
            default:
                System.out.println("잘못된 입력입니다.");
                return;
        }
        shapes.add(shape);
    }

    public void delete() {
        System.out.println("삭제할 도형의 위치 >> ");
        int position = scanner.nextInt();
        scanner.nextLine(); // consume the leftover newline

        if (position < 1 || position > shapes.size()) {
            System.out.println("삭제할 수 없습니다.");
        } else {
            shapes.remove(position - 1);
        }
    }

    public void showAll() {
        for (Shape shape : shapes) {
            shape.draw();
        }
    }

    public void run() {
        System.out.println("그래픽 에디터 beauty을 실행합니다.");
        while (true) {
            System.out.println("삽입(1), 삭제(2), 모두 보기(3), 종료(4) >> ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // consume the leftover newline

            switch (choice) {
                case 1:
                    insert();
                    break;
                case 2:
                    delete();
                    break;
                case 3:
                    showAll();
                    break;
                case 4:
                    System.out.println("beauty을 종료합니다.");
                    return;
                default:
                    System.out.println("잘못된 입력입니다.");
            }
        }
    }

    public static void main(String[] args) {
        GraphicEditor editor = new GraphicEditor();
        editor.run();
    }
}

 

💡 

 

[정답]

import java.util.Scanner;
import java.util.Vector;

class Nation {
    private String country;
    private String capital;

    public Nation(String country, String capital) {
        this.country = country;
        this.capital = capital;
    }

    public String getCountry() {
        return country;
    }

    public String getCapital() {
        return capital;
    }
}

class NationGame {
    private Vector<Nation> v = new Vector<>();
    private Scanner scanner = new Scanner(System.in);

    public NationGame() {
        // 9개의 나라와 수도를 입력
        v.add(new Nation("대한민국", "서울"));
        v.add(new Nation("미국", "워싱턴"));
        v.add(new Nation("일본", "도쿄"));
        v.add(new Nation("중국", "베이징"));
        v.add(new Nation("러시아", "모스크바"));
        v.add(new Nation("영국", "런던"));
        v.add(new Nation("프랑스", "파리"));
        v.add(new Nation("독일", "베를린"));
        v.add(new Nation("이탈리아", "로마"));
    }

    public void run() {
        System.out.println("**** 수도 맞추기 게임을 시작합니다. ****");
        while (true) {
            System.out.print("입력:1, 퀴즈:2, 종료:3>> ");
            if (!scanner.hasNextInt()) {
                System.out.println("잘못된 입력입니다. 다시 시도하세요.");
                scanner.next(); // 잘못된 입력을 소비합니다.
                continue;
            }
            int menu = scanner.nextInt();
            scanner.nextLine(); // 개행 문자 소비

            switch (menu) {
                case 1:
                    input();
                    break;
                case 2:
                    quiz();
                    break;
                case 3:
                    System.out.println("게임을 종료합니다.");
                    return;
                default:
                    System.out.println("잘못된 입력입니다. 다시 시도하세요.");
            }
        }
    }

    public void input() {
        System.out.print("현재 " + v.size() + "개 나라와 수도가 입력되어 있습니다.\n나라와 수도 입력 " +
                "(예: 대한민국 서울)>> ");
        String nation = scanner.next();
        String capital = scanner.next();

        // 나라가 이미 있는지 확인 후 중복된다면 추가 X
        for (Nation n : v) {
            if (n.getCountry().equals(nation)) {
                System.out.println(nation + "은 이미 있습니다.");
                return;
            }
        }
        v.add(new Nation(nation, capital));
    }

    public void quiz() {
        while (true) {
            int index = (int) (Math.random() * v.size());
            Nation nation = v.get(index);
            System.out.print(nation.getCountry() + "의 수도는? ");
            String answer = scanner.next();
            if (answer.equals("그만")) {
                break;
            }
            if (answer.equals(nation.getCapital())) {
                System.out.println("정답!!");
            } else {
                System.out.println("아닙니다!!");
            }
        }
    }
}

public class DaraSolution {
    public static void main(String[] args) {
        NationGame game = new NationGame();
        game.run();
    }
}

 

반응형
profile

다라다라V

@DaraDaraV

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!