과제 3

2025. 2. 1. 14:48·과제

설연휴 java 코딩 과제

package re_html;

import java.util.*;

public class homework {

	public static void main(String[] args) {
		homework_box hb = new homework_box();
		hb.gen_rand();
		hb.match_num();
		hb.papagu();
		hb.input_sort();
		hb.biggest();
	}
	
}


class homework_box{
	Scanner sc = null;
	ArrayList<ArrayList<String>> sarr2 = null;
	ArrayList<Integer> unum = null;
	ArrayList<String> sarr = null;
	ArrayList<Integer> iarr = null;
	Integer no = null;
	int cnt = 0;	// 맞춘 개수 

	/*
	 응용문제1
	 다음 사용자가 원하는 숫자를 입력하게 되면, 해당 숫자 만큼 난수가 자동으로 생성 되어야 합니다.
	 예) 숫자를 입력 하세요 : 5      
	 결과 : 10,22,34,7,12
	 중복 x
	 난수의 범위는 1~30까지 입니다. 
	 */
	public void gen_rand() {
		this.sc = new Scanner(System.in);
		this.iarr = new ArrayList<Integer>();
		int user = 0;
		System.out.println("생성할 난수의 개수 : ");
		try {
			user = this.sc.nextInt();
			int i = 0;
			int j = 0;

			while (i < user) {
				this.iarr.add((int) Math.ceil(Math.random() * 30));

				while (j < this.iarr.size()) {
					if (this.iarr.get(i) == this.iarr.get(j)) {
						this.iarr.set(i, (int) Math.ceil(Math.random() * 46));
					}
					j++;
				}
				i++;
			}
			System.out.println(this.iarr);
		} catch (Exception e) {
			System.out.println("잘못된 입력입니다.");
			new homework_box().gen_rand();
		}finally {
			this.sc.close();
		}

	}

	/* 응용문제2 
	 * 사용자가 6개의 숫자를 입력합니다. 
	 * 총 몇개의 숫자를 맞췄는지를 출력하는 코드를 작성하시오. 
	 * 만약 사용자가 6개의 숫자를
	 * 입력하지 않을 경우 무조건 6개의 숫자를 입력하도록 합니다. 
	 * (동일한 숫자는 사용하지 못함)
	 * [입력 예시] 숫자를 입력하세요(1~46) : 3,5,22,35,34,22
	 */
	public void match_num() {
		Integer num[] = {1,2,3,4,5,6};
		this.sc = new Scanner(System.in);
		this.iarr = new ArrayList<Integer>(Arrays.asList(num));
		this.unum = new ArrayList<Integer>();
		int ea = 6;		// 입력 개수
		int i = 0;
		try {
			System.out.println("숫자 6개 맞추기");
			while (i < ea) { // 입력 반복문
				System.out.println("숫자 입력 (1~46) : ");

				this.no = sc.nextInt();
				if (check_range() == true) {
					i++;
				}
			}
			this.print_mn();
		} catch (Exception e) {
			System.out.println("잘못된 입력입니다.");
			new homework_box().match_num();
		}finally {
			this.sc.close();
		}
	}
	
	private boolean check_range() {
		boolean ck = false;
		if (this.is_dupl(this.no) == true) { // 중복이면
			System.out.println("중복된 숫자입니다");
		} else if (this.no < 1 || this.no > 46) { // 중복아니면 사용자배열에 넣기
			System.out.println("범위에 맞는 숫자를 입력해주세요");
		} else {
			ck = true;
			this.unum.add(this.no);
		}
		return ck;
		
	}
	
	private boolean is_dupl(Integer no) { // 비교할 숫자 입력 -> 중복시 true
		boolean ck = false;
		int w = 0;
		while (w < this.unum.size()) {
			if (no == this.unum.get(w)) {
				ck = true;
			}
			w++;
		}
		return ck;
	}
	
	private void print_mn() {
		int i = 0;
		int j = 0;
		while (i < this.iarr.size()) {
			j = 0;
			while (j < this.iarr.size()) {
				if (this.iarr.get(i) == unum.get(j)) {
					cnt++;
				}
				j++;
			}
			i++;
		}
		System.out.println("사용자가 입력한 6개의 숫자\n" + this.unum);
		System.out.println("컴퓨터의 숫자 6개\n" + this.iarr);
		System.out.println("맞춘 개수 : " + cnt);

	}
	
	/* 응용문제3
	AI 번역기 프로그램을 제작하게 됩니다. 해당 단어가 입력시 한글로 번역되록 하시오.
	[영어 데이터 내용]
	watermelon : 수박
	kiwi : 키위
	grape : 포도
	strawberry : 딸기

	[입력 및 출력 형태]
	과일 이름을 한글로 입력하세요 :  포도
	영어 : grape
	 */
	public void papagu() {
		String data[][] = {
				{"watermelon","수박"},
				{"kiwi","키위"},
				{"grape","포도"},
				{"strawberry","딸기"}
		};
		sarr2 = new ArrayList<ArrayList<String>>();
		String user = "";
		int cnt = 0;
		int i = 0;
		while(i<data.length) {
			sarr = new ArrayList<String>(Arrays.asList(data[i]));
			sarr2.add(sarr);
			i++;
		}
		this.sc = new Scanner(System.in);
		
		try {
			System.out.println("과일 이름을 한글로 입력하세요 : ");
			user = sc.next();
			i = 0;
			while (i < sarr2.size()) {
				if (sarr2.get(i).get(1).indexOf(user) != -1) {
					System.out.println("영어 : " + sarr2.get(i).get(0));
					cnt++;
				}
				i++;
			}
			if (cnt == 0) {
				System.out.println("배열에 없는 단어입니다.");
			}
		} catch (Exception e) {
			System.out.println("오류 발생");
			new homework_box().papagu();
		}finally {
			this.sc.close();
		}
	}
	
	/* 응용문제4
	사용자가 무작위 데이터 숫자를 입력합니다. 
	단, 입력 범위는 1~500 까지 입니다. 
	총 10번을 입력하며 입력된 숫자를 내림차순 으로 정렬하는 코드를 작성하시오.
	[입력사항]
	[7,37,22,88,68,51,52,40,21,38]
	[최종 출력]
	[88,68,52,51,40,38,37,22,21,7]
	 */
	
	public void input_sort() {
		this.iarr = new ArrayList<Integer>();
		this.sc = new Scanner(System.in);
		int ea = 10;
		int i = 0;
		
		try {
			while (i < ea) { // 입력 반복문
				System.out.println("숫자 입력 (1~500) : ");
				this.no = sc.nextInt();

				if (this.no >= 1 && this.no <= 500) {
					this.iarr.add(this.no);
					i++;
				} else {
					System.out.println("범위에 맞는 숫자를 입력해주세요");
				}
			}
			Collections.sort(this.iarr,Collections.reverseOrder());
			System.out.println(this.iarr);
		}catch(Exception e) {
			System.out.println("오류발생");
			new homework_box().input_sort();
		}finally {
			this.sc.close();
		}
		
		
	}
	
	/* 응용문제5
	다음 배열 값중 가장 큰값이 출력 되도록 합니다.
	[20,36,11,8,22,31]
	결과
	가장 큰 수는 36 입니다.
	 */
	public void biggest() {
		Integer arr[] = {20,36,11,8,22,31};
		this.iarr = new ArrayList<Integer>(Arrays.asList(arr));
		Collections.sort(this.iarr,Collections.reverseOrder());
		System.out.println("가장 큰 수는 "+this.iarr.get(0)+"입니다.");
	}
}

⭐️try ~ catch 

캐치에서 재귀시 출력부분 신경쓰기

저작자표시 비영리 변경금지 (새창열림)
'과제' 카테고리의 다른 글
  • 과제 2
  • 과제 1
9na0
9na0
응애
  • 9na0
    구나딩
    9na0
  • 전체
    오늘
    어제
    • 분류 전체보기 (210)
      • Web (118)
      • Java (28)
      • 데이터베이스 (14)
      • 세팅 (12)
      • 과제 (3)
      • 쪽지시험 (2)
      • 정보처리기사 (4)
      • 서버 (24)
  • 블로그 메뉴

    • 링크

      • 포폴
      • 구깃
    • 공지사항

    • 인기 글

    • 태그

      spring-boot
      io_dto
      net3
      exam1_1~10
      noticewriteok
      re2
      net1
      java_io1~10
      net2
      ab1
      re_java10
      notice_writer
      file25
      net5~10
      macbook pro m4
      Oracle
      datalist
      net4
      file24
      file25_t
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.3
    9na0
    과제 3
    상단으로

    티스토리툴바