1. Scanner 클래스를 이용하여 원화를 입력받아 달러로 바꾸어 다음 예시와 같아 출력하는 프로그램을 작성하라, $1 = 1100원으로 가정하고 계산하라.
[결과]
[소스 코드]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("원화를 입력하세요(단위 원) >> ");
float won = input.nextFloat();
System.out.println(won + "원은 $" + won / 1100.0 + "입니다.");
}
}
2. Scanner 클래스를 이용하여 2자리의 정수(10 ~ 99사이)를 입력받고, 십의 자리와 1의 자리가 같은지 판별하여 출력하는 프로그램을 작성하라.
[결과]
[소스 코드]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("2자리수 정수 입력(10 ~ 99) >> ");
int num = input.nextInt();
int a = num / 10;
int b = num % 10;
if(a == b)
System.out.println("Yes!! 10의 자리와 1의 자리가 같습니다.");
else
System.out.println("No!! 10의 자리와 1의 자리가 같지 않습니다.");
}
}
3. Scanner 클래스를 이용하여 정수로 된 돈의 액수를 입력받아 오만 원권, 만 원권, 천 원권, 500원 짜리 동전, 50원 짜리 동전, 10원 짜리 동전, 1원 짜리 동전 각 몇개로 변환되는지를 출력하라.
[결과]
[소스 코드]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("금액을 입력하시오. >> ");
int money = input.nextInt();
int 오만원권 = 0, 만원권 = 0, 천원권 = 0, 백원 = 0, 오십원 = 0, 십원 = 0, 일원 = 0;
int cnt = 0;
if(money > 50000) {
cnt = money / 50000;
money -= 50000 * cnt;
오만원권 += cnt;
}
if(money > 10000) {
cnt = money / 10000;
money -= 10000 * cnt;
만원권 += cnt;
}
if(money > 1000) {
cnt = money / 1000;
money -= 1000 * cnt;
천원권 += cnt;
}
if(money > 100) {
cnt = money / 100;
money -= 100 * cnt;
백원 += cnt;
}
if(money > 50) {
cnt = money / 50;
money -= 50 * cnt;
오십원 += cnt;
}
if(money > 10) {
cnt = money / 10;
money -= 10 * cnt;
십원 += cnt;
}
if(money > 1) {
cnt = money % 10;
money -= 10 * cnt;
일원 += cnt;
}
System.out.println("오만원권 " + 오만원권 +"매");
System.out.println("만원권 " + 만원권 +"매");
System.out.println("천원원권 " + 천원권 +"매");
System.out.println("백원 " + 백원 +"개");
System.out.println("오십원 " + 오십원 +"개");
System.out.println("십원 " + 십원 +"개");
System.out.println("일원 " + 일원 +"개");
}
}
4. Scanner 클래스로 정수 3개를 입력받고 3개의 숫자 중 중간 크기의 수를 출력하라. 평균값을 구하는 것이 아님에 주의하라.
[결과]
[소스 코드]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("정수 3개 입력 >> ");
int a = 0, b = 0, c = 0;
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
if(a > b && a > c) {
if(b > c)
System.out.println("중간 값은 " + b);
else if(b < c)
System.out.println("중간 값은 " + c);
}
else if(b > a && b > c) {
if(a > c)
System.out.println("중간 값은 " + a);
else if(a < c)
System.out.println("중간 값은 " + c);
}
else if(c > a && c > b) {
if(a > b)
System.out.println("중간 값은 " + a);
else if(a < b)
System.out.println("중간 값은 " + b);
}
}
}
5. Scanner를 이용하여 삼각형의 변의 길이를 나타내는 정수를 3개 입력받고, 이 3개의 수로 삼각형을 만들 수 있는지 판별하라. 삼각형이 되려면 두 변의 합이 다른 한 변의 합보다 커야한다.
[결과]
[소스 코드]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("정수 3개를 입력하시오 >> ");
int a = 0, b = 0, c = 0;
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
int max = a, x = b, y = c;
if(max < b) { max = b; x = a; y = c; }
else if(max < c) { c = max; x = a; y = b; }
if(max < x + y)
System.out.println("삼각형이 됩니다.");
else
System.out.println("삼각형이 안 됩니다.");
}
}
6. 369 게임을 간단히 작성해보자. 1 ~ 99 까지의 정수를 입력받고 정수에 3, 6, 9 중 하나가 있는 경우는 "박수짝"을 출력하고 두 개 있는 경우는 "박수짝짝"을 출력하는 프로그램을 작성하라. 예를 들면, 키보드로 입력된 수가 13인 경우 "박수짝"을, 36인 경우 "박수짝짝"을 출력하면 된다.
[결과]
[소스 코드]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("1 ~ 99 사이의 정수를 입력하시오. >> ");
int num = input.nextInt();
int b = num % 10;
num -= b;
int a = num / 10;
if(a % 3 == 0 || a % 6 == 0 || a % 9 == 0) {
if(b % 3 == 0 || b % 6 == 0 || b % 9 == 0) {
System.out.println("박수짝짝");
}
else System.out.println("박수짝");
}
else if(b % 3 == 0 || b % 6 == 0 || b % 9 == 0)
System.out.println("박수짝");
}
}
7. 2차원 평면에서 직사각형은 왼쪽 상단 모서리와 오른쪽 하단 모서리의 두 점으로 표현한다. (100, 100)과 (200, 200)의 두 점으로 이루어진 사각형이 있을 때, Scanner를 이용하여 정수 x와 y 값을 입력받고 점 (x, y)가 이 직사각형 안에 있는지를 판별하는 프로그램을 작성하라.
[결과]
[소스 코드]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("점 (x, y)의 좌표를 입력하시오 >> ");
int x = input.nextInt();
int y = input.nextInt();
if(x >= 100 && x <= 200 && y >= 100 && y <= 200)
System.out.println("(" + x + ", " + y + ")는 사각형 안에 있습니다.");
else System.out.println("(" + x + ", " + y + ")는 사각형 안에 없습니다.");
}
}
8. 2차원 평면에서 직사각형은 문제 7번처럼 두 점으로 표현된다. 키보드로부터 직사각형을 구성하는 두 점 (x1, y1), (x2, y2)를 입력받아 (100, 100), (200, 200)의 두 점으로 이루어진 직사각형과 충돌하는지 판별하는 프로그램을 작성하라.
[결과]
[소스 코드]
import java.util.Scanner;
public class App{
public static boolean intRect(int x, int y, int rectx1, int recty1, int rectx2, int recty2) {
if((x >= rectx1 && x <= rectx2) && (y >= recty1 && y <= recty2))
return true;
else return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("두 점 (x1, y1)과 (x2, y2)의 좌표를 입력하시오 >> ");
int x1 = input.nextInt();
int y1 = input.nextInt();
int x2 = input.nextInt();
int y2 = input.nextInt();
if(intRect(x1, y1, 100, 100, 200, 200) || intRect(x2, y2, 100, 100, 200, 200))
System.out.println("충돌합니다.");
else System.out.println("충돌하지 않습니다.");
}
}
9. 원의 중심을 나타내는 한 점과 반지름을 실수 값으로 입력받아라. 그리고 실수 값으로 다른 점 (x, y)를 입력받아 이 점이 원의 내부에 있는지를 판별하여 출력하라,
[결과]
[소스 코드]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("원의 중심과 반지름 입력 >> ");
double rx = input.nextDouble();
double ry = input.nextDouble();
double radius = input.nextDouble();
System.out.print("점 입력 >> ");
double x = input.nextDouble();
double y = input.nextDouble();
double X = Math.pow(x - rx, 2);
double Y = Math.pow(y - ry, 2);
double length = Math.sqrt(X + Y);
if(length <= radius)
System.out.println("점 (" + x + ", " + y + ")는 원 안에 있다.");
else System.out.println("점 (" + x + ", " + y + ")는 원 안에 없다.");
}
}
10. 원의 정보를 받기 위해 키보드로부터 원의 중심을 나타내는 한 점과 반지름을 입력받는다. 두 개의 원을 입력받고 두 원이 서로 겹치는지를 판단하여 출력하라.
[결과]
[소스 코드]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("첫번째 원의 중심과 반지름 입력 >> ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double radius1 = input.nextDouble();
System.out.print("두번째 원의 중심과 반지름 입력 >> ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double radius2 = input.nextDouble();
double length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
if(length <= radius1 + radius2)
System.out.println("두 원이 겹칩니다.");
else System.out.println("두 원이 겹치지 않습니다.");
}
}
11. 숫자를 입력받아 3 ~ 5는 "봄, 6 ~ 8은 "여름", 9 ~ 11은 "가을, 12, 1, 2의 경우 "겨울"을, 그 외 숫자를 입력한 경우 "잘못입력"을 출력하는 프로그램을 작성하라.
[결과]
[(1) if-else 문을 이용하여 프로그램을 작성하라.]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("달을 입력하세요 (1 ~ 12) >> ");
int month = input.nextInt();
if(month >= 3 && month <= 5)
System.out.println("봄");
else if(month >= 6 && month <= 8)
System.out.println("여름");
else if(month >= 9 && month <= 11)
System.out.println("가을");
else if(month == 12 || month == 1 || month == 2)
System.out.println("겨울");
else System.out.println("잘못입력");
}
}
[(2) switch 문을 이용하여 프로그램을 작성하라.]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("달을 입력하세요 (1 ~ 12) >> ");
int month = input.nextInt();
switch(month) {
case 3:
case 4:
case 5:
System.out.println("봄");
break;
case 6:
case 7:
case 8:
System.out.println("여름");
break;
case 9:
case 10:
case 11:
System.out.println("가을");
break;
case 12:
case 1:
case 2:
System.out.println("겨울");
break;
default:
System.out.println("잘못입력");
break;
}
}
}
12. 사칙 연산을 입력받아 계산하는 프로그램을 작성하고자 한다. 연산자는 +, =, *, /의 네 가지로 하고 피연산자는 모두 실수로 한다. 피연산자와 연산자는 실행 사례와 같이 빈칸으로 분리하여 입력한다. 0으로 나누기 시 "0으로 나눌 수 없습니다."를 출력하고 종료한다.
[결과]
[(1) if-else 문을 이용하여 프로그램을 작성하라.]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("연산 >> ");
int x = input.nextInt();
String op = input.next();
int y = input.nextInt();
if(op.equals("+")) {
System.out.println(x + " + " + y +"의 계산 결과는 " + (x + y));
}
else if(op.equals("=")) {
if(x == y)
System.out.println("x와 y는 같습니다.");
else System.out.println("x와 y는 같지 않습니다.");
}
else if(op.equals("*")) {
System.out.println(x + " x " + y +"의 계산 결과는 " + (x * y));
}
else if(op.equals("/")) {
if(y == 0)
System.out.println("0으로 나눌 수 없습니다.");
else System.out.println(x + " / " + y +"의 계산 결과는 " + (x / y));
}
}
}
[(2) switch 문을 이용하여 프로그램을 작성하라.]
import java.util.Scanner;
public class App{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("연산 >> ");
int x = input.nextInt();
String op = input.next();
int y = input.nextInt();
switch(op) {
case "+":
System.out.println(x + " + " + y +"의 계산 결과는 " + (x + y));
break;
case "=":
String equals = (x == y) ? "x와 y는 같습니다." : "x와 y는 같지 않습니다.";
System.out.println(equals);
break;
case "*":
System.out.println(x + " x " + y +"의 계산 결과는 " + (x * y));
break;
case "/":
String check = (y == 0) ? "0으로 나눌 수 없습니다." : x + " / " + y +"의 계산 결과는 " + (x / y);
System.out.println(check);
break;
}
}
}
'JAVA > 명품 JAVA Programming' 카테고리의 다른 글
[(개정판 4판) 명품 JAVA Programming] 3장 실습 문제 (0) | 2023.01.24 |
---|---|
[(개정판 4판) 명품 JAVA Programming] 3장 이론 문제 (0) | 2023.01.23 |
[(개정판 4판) 명품 JAVA Programming] 2장 이론 문제 (0) | 2023.01.22 |
[(개정판 4판) 명품 JAVA Programming] 1장 이론 문제 (0) | 2023.01.17 |
[(개정판 4판) 명품 JAVA Programming] 1장 실습 문제 (0) | 2023.01.17 |