본문 바로가기
JAVA

명품 자바 에센셜 3강 실습문제

by IT 정복가 2022. 3. 10.
728x90

1. 영문 소문자를 하나 입력받고 그 문자보다 알파벳 순위가 낮은 모든 문자를 출력하는 프로그램을 작성하라.

답:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("알파벳 한 문자를 입력하세요>>");
		String s = sc.next();
		char c = s.charAt(0);
		
		for (int i = 0; i <= c-'a'; i++) {
			for (int j = i; j <= c-'a'; j++) {
				System.out.print((char)('a'+j));
			}
			System.out.println();
		}
		sc.close();
	}

}

2. 정수를 10개 입력받아 배열에 저장한 후, 배열을 검색하여 3의 배수만 출력하는 프로그램을 작성하라.

정수 10개 입력>>2 44 77 6 8 9 12 88 100 2323
6 9 12

답:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int arr[] = new int[10];
		System.out.print("정수 10개 입력>>");
		
		for(int i=0; i < arr.length; i++) {
			arr[i] = sc.nextInt();
			if(arr[i]%3 == 0) {
				System.out.print(arr[i]+" ");
			}
		}	
		sc.close();
	}

}

3. 정수를 입력받아 짝수이면 “짝”, 홀수이면 “홀”을 출력하는 프로그램을 작성하라. 사용자가 정수를 입력하지 않는 경우에는 프로그램을 종료하라. 정답을 통해 try-catch-finally를 사용하는 정통적인 코드를 볼 수 있다.

정수를 입력하세요>>352
짝수
정수를 입력하세요>>JAVA
수를 입력하지 않아 프로그램을 종료합니다.

답:

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("정수를 입력하세요>>");
		
		try {
			int num = sc.nextInt();
			if(num%2 == 0)
				System.out.println("짝수");
			else
				System.out.println("홀수");
		}
		catch(InputMismatchException e) {
			System.out.println("수를 입력하지 않아 프로그램을 종료합니다.");
		}
		
		finally {
			sc.close();
		}
	}

}

4. ‘일’, ‘월’, ‘화’, ‘수’, ‘목’, ‘금’, ‘토’로 초기화된 문자 배열 day를 선언하고, 사용자로부터 정수를 입력받아 7(배열 day의 크기)로 나눈 나머지를 인덱스로 하여 배열 day에 저장된 문자를 출력하라. 음수가 입력되면 프로그램을 종료하라. 아래 실행 결과와 같이 예외 처리하라.

답:

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		char day[] = {'일', '월', '화', '수', '목', '금', '토'};
		
		while (true) {
			System.out.print("정수를 입력하세요>>");

			try {
				int a = sc.nextInt();
				if(a < 0) {
					System.out.println("프로그램을 종료합니다...");
					break;
				}
				a %= 7;
				System.out.println(day[a]);
				}
			catch(InputMismatchException e){
				System.out.println("경고! 수를 입력하지 않았습니다.");
				sc.next();
			}
		}
		sc.close();	
	}
}

5. 정수를 10개 입력받아 배열에 저장하고 증가 순으로 정렬하여 출력하라.

정수 10개 입력>>17 3 9 -6 77 234 5 23 -3 1
-6 -3 1 3 5 9 17 23 77 234

답:

import java.util.*;

public class java {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int arr[] = new int[10];
		int a;
		
		System.out.print("정수 10개 입력>>");
		for(int i=0; i<arr.length; i++) {
			arr[i] = sc.nextInt();
		}
		for(int i=0; i<arr.length; i++) {
			for(int j=0; j < i; j++) {
				if(arr[j]>arr[i]) {
					a = arr[i];
					arr[i] = arr[j];
					arr[j] = a;
				}
			}
		
		}
		for(int i : arr) {
			System.out.print(i+" ");
		}
		sc.close();
	}

}

6. 다음과 같이 영어와 한글의 짝을 이루는 2개의 배열을 만들고, 사용자로부터 영어 단어를 입력받아 출력하는 프로그램을 작성하라. “exit”을 입력하면 프로그램을 종료하라.

답:

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		String eng[] = {"student","love","java","happy","future"};
		String kor[] = {"학생","사랑","자바","행복한","미래"};
		
		while(true)
		{
			System.out.print("영어 단어를 입력하세요>>");
			int i;
			String s = sc.next();
			
			if(s.equals("exit")) break;
			
			for (i = 0; i < eng.length; i++) {
				if(s.equals(eng[i])) {
					System.out.println(kor[i]);
					break;
				}
				if(i == eng.length - 1) System.out.println("그런 영어 단어가 없습니다.");
			}
		}
		System.out.println("종료합니다...");
	}
}

7. 1부터 99까지, 369게임에서 박수를 쳐야 하는 경우, 순서대로 화면에 출력하라. 2장 실습 문제 9를 참고하라.

답:

public class Main {

	public static void main(String[] args) {
		for(int i = 1; i < 100; i++) {
			int first = i/10;
			int second = i%10;
			
			if(first == 3 || first == 6 || first == 9) {
				if(second == 3 || second == 6 || second == 9)
					System.out.println(i+"박수두번");
				else if (second != 3 || second != 6 || second != 9)
					System.out.println(i+"박수한번");
			}
			else if(first != 3 || first != 6 || first != 9) {
				if(second == 3 || second == 6 || second == 9)
					System.out.println(i+"박수한번");
			}
		}
	}
}

8. 컴퓨터와 사용자의 가위바위보 게임 프로그램을 작성하라. 사용자가 입력하고 <Enter> 키를 치면, 컴퓨터는 랜덤 하게 가위, 바위, 보 중 하나를 선택한다. 그리고 누가 이겼는지 출력한다. “그만”을 입력하면 게임을 종료한다.

답:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		String str[] = {"가위", "바위", "보"};
		System.out.println("컴퓨터와 가위 바위 보 게임을 합니다.");
		
		while(true) {
			int n = (int)(Math.random()*3); // 0, 1, 2 중에 랜덤 정수 리턴
			System.out.print("가위 바위 보!>>");
			String game = sc.next();
			if(game.equals("그만")) {
				System.out.println("게임을 종료합니다...");
				break;
			}
			
			if(str[n].equals("가위")) {
				if(game.equals("가위")) 
					System.out.println("사용자 = "+game+" , "+"컴퓨터 = "+str[n]+", 비겼습니다.");
				else if(game.equals("바위"))
					System.out.println("사용자 = "+game+" , "+"컴퓨터 = "+str[n]+", 사용자가 이겼습니다.");
				else if(game.equals("보"))
					System.out.println("사용자 = "+game+" , "+"컴퓨터 = "+str[n]+", 컴퓨터가 이겼습니다.");
			}
			if(str[n].equals("바위")) {
				if(game.equals("가위")) 
					System.out.println("사용자 = "+game+" , "+"컴퓨터 = "+str[n]+", 컴퓨터가 이겼습니다.");
				else if(game.equals("바위"))
					System.out.println("사용자 = "+game+" , "+"컴퓨터 = "+str[n]+", 비겼습니다.");
				else if(game.equals("보"))
					System.out.println("사용자 = "+game+" , "+"컴퓨터 = "+str[n]+", 사용자가 이겼습니다.");
			}
			if(str[n].equals("보")) {
				if(game.equals("가위")) 
					System.out.println("사용자 = "+game+" , "+"컴퓨터 = "+str[n]+", 사용자가 이겼습니다.");
				else if(game.equals("바위"))
					System.out.println("사용자 = "+game+" , "+"컴퓨터 = "+str[n]+", 컴퓨터가 이겼습니다.");
				else if(game.equals("보"))
					System.out.println("사용자 = "+game+" , "+"컴퓨터 = "+str[n]+", 비겼습니다.");
			}
		}
		sc.close();
	}
}

음...너무 무식하게 했나...언제쯤 코딩머리가 생기려나

728x90