1. Scanner 클래스를 이용하여 원화를 입력받아 달러로 바꾸어 다음 예시와 같이 출력하는 프로그램을 작성하라.
$1=1100원으로 가정하고 계산하라
CODE CLOSE
import java.util.Scanner ;
public class _01 {
public static void main(String [] args) {
Scanner scanner = new Scanner (System .in );
System .out .print ("원화를 입력하세요(단위 원)>>" );
int won = scanner.nextInt();
double dollar = won/ 1100 ;
System .out .println (won+ "원은 $" + dollar+ "입니다." );
scanner.close();
}
}
cs
CLOSE
2. Scanner 클래스를 이용하여 2자리의 정수(10~99사이)를 입력받고, 십의 자리와 1의 자리가 같은지 판별하여 출력하는 프로그램을 작성하라.
CODE CLOSE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Scanner ;
public class _02 {
public static void main(String [] args) {
Scanner scanner = new Scanner (System .in );
System .out .print ("2자리수 정수 입력(10~99)>>" );
int n = scanner.nextInt();
if (n> = 10 & & n< = 99 )
{
if (n/ 10 = = n%10 )
{
System .out .println ("Yes! 10의 자리와 1의 자리가 같습니다." );
}
else
{
System .out .println ("No! 10의 자리와 1의 자리가 다릅니다." );
}
}
else
{
System .out .println ("범위 내의 정수를 입력하시오." );
}
scanner.close();
}
}
cs
CLOSE
3. Scanner 클래스를 이용하여 정수로 된 돈의 액수를 입력받아 오만 원권, 만 원권, 천 원권, 500원짜리 동전, 100원짜리 동전, 50원짜리 동전, 10원짜리 동전, 1원짜리 동전 각 몇 개로 변환되는지 출력하라.
CODE CLOSE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
public class _03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("금액을 입력하시오>>");
int m = scanner.nextInt();
System.out.println("오만원권 "+m/50000+"매");
System.out.println("만원권 "+m%50000/10000+"매");
System.out.println("천원권 "+m%50000%10000/1000+"매");
System.out.println("백원 "+m%50000%10000%1000/100+"매");
System.out.println("오십원 "+m%50000%10000%1000%100/50+"매");
System.out.println("십원 "+m%50000%10000%1000%100%50/10+"매");
System.out.println("일원 "+m%50000%10000%1000%100%50%10/1+"매");
scanner.close();
}
}
cs
CLOSE
4. Scanner 클래스로 정수 3개를 입력받고 3개의 숫자 중 중간 크기의 수를 출력하라. 평균값을 구하는 것이 아님에 주의하라.
CODE CLOSE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Scanner ;
public class _04 {
public static void main(String [] args) {
Scanner scanner = new Scanner (System .in );
System .out .print ("정수 3개를 입력>>" );
int array[]= new int [3 ];
int tmp= 0 ;
for (int i= 0 ;i< array.length ;i+ + )
{
array[i]= scanner.nextInt();
}
for (int i= 0 ;i< array.length ;i+ + )
{
for (int j= 0 ;j< array.length - 1 ;j+ + )
{
if (array[j]> array[j+ 1 ])
{
tmp= array[j];
array[j]= array[j+ 1 ];
array[j+ 1 ]= tmp;
}
}
}
System .out .println ("중간 값은 " + array[1 ]+ "입니다." );
}
}
cs
CLOSE
5. Scanner를 이용하여 삼각형의 변의 길이를 나타내는 정수를 3개 입력받고 이 3개의 수로 삼각형을 만들 수 있는지 판별하라. 삼각형이 되려면 두 변의 합이 다른 한 변의 합보다 커야 한다.
CODE CLOSE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Scanner;
public class _05 {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
System.out.print("정수 3개를 입력하시오>>");
int[] array = new int[3];
for(int i=0;i<array.length;i++)
{
array[i]=scanner.nextInt();
}
if(array[0]+array[1]>array[2] && array[0]+array[2]>array[1] && array[1]+array[2]>array[0])
{
System.out.println("삼각형이 됩니다.");
}
else
System.out.println("삼각형이 되지 않습니다.");
scanner.close();
}
}
cs
CLOSE
6. 369게임을 간단히 작성해보자. 1~99까지의 정수를 입력받고 정수에 3,6,9 중 하나가 있는 경우는 "박수짝"을 출력하고 두 개 있는 경우는 "박수짝짝"을 출력하는 프로그램을 작성하라. 예를 들면, 키보드로 입력된 수가 13인 경우 "박수짝"을, 36인 경우 "박수짝짝"을 출력하면 된다.
CODE CLOSE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;
public class _06 {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("1~99 사이의 정수를 입력하시오>>");
int n = scanner.nextInt();
if(n/10==3 || n/10==6 || n/10==9)
{
if(n%10==3 || n%10==6 || n%10==9)
{
System.out.println("박수짝짝");
}
else
System.out.println("박수짝");
}
else
System.out.println("박수없음");
scanner.close();
}
}
cs
CLOSE
7. 2차원 평면에서 직사각형은 왼쪽 상단 모서리와 오른쪽 하단 모서리의 두 점으로 표현한다. (100,100)과 (200,200)의 두 점으로 이루어진 사각형이 있을 때,
Scanner를 이용하여 정수 x와 y값을 입력받고 점(x,y)가 이 직사각형 안에 있는지를 판별하는 프로그램을 작성하라.
CODE CLOSE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner ;
public class _07 {
public static void main(String [] args) {
Scanner scanner = new Scanner (System .in );
System .out .print ("점 (x,y)의 좌표를 입력하시오>>" );
int x= scanner.nextInt();
int y= scanner.nextInt();
if (x> 100 & & x< 200 & & y> 100 & & y< 200 )
{
System .out .println ("(" + x+ "," + y+ ")는 사각형 안에 있습니다." );
}
else
System .out .println ("(" + x+ "," + y+ ")는 사각형 안에 있지 않습니다." );
scanner.close();
}
}
cs
CLOSE
8. 2차원 평면에서 직사각형은 문제 7번처럼 두 점으로 표현된다. 키보드로부터 직사각형을 구성하는 두 점 (x1,y1), (x2,y2)를 입력받아 (100,100), (200,200)의 두 점으로 이루어진 직사각형과 충돌하는지 판별하는 프로그램을 작성하라.
CODE CLOSE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner ;
public class _08 {
public static void main(String [] args)
{
Scanner scanner = new Scanner (System .in );
System .out .print ("점 (x1,y1),(x2,y2)의 좌표를 입력하시오>>" );
int x1= scanner.nextInt();
int y1= scanner.nextInt();
int x2= scanner.nextInt();
int y2= scanner.nextInt();
if (inRect(x1,y1,x2,y2,100 ,100 ,200 ,200 ) = = true )
{
System .out .println ("(" + x1+ "," + y1+ ")과(" + x2+ "," + y2+ ")는 사각형 안에 있습니다." );
}
else
System .out .println ("(" + x1+ "," + y1+ ")+과 +(" + x2+ "," + y2+ ")는 사각형 안에 없습니다." );
scanner.close();
}
public static boolean inRect(int x1, int y1,int x2,int y2 ,int rectx1, int recty1, int rectx2, int recty2) {
if ((x1> = rectx1 & & x1< = rectx2) & & (x2> = rectx1 & & x2< = rectx2) & & (y1> = recty1 & & y1< = recty2) & & (y2> = recty1 & & x1< = recty2))
return true ;
else
return false ;
}
}
cs
CLOSE
9. 원의 중심을 나타내는 한 점과 반지름을 실수 값으로 입력받아라. 그리고 실수 값으로 다른 점 (x,y)를 입력받아 이 점이 원의 내부에 있는지 판별하여 출력하라.
CODE CLOSE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
public class _09 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("원의 중심과 반지름 입력>>");
int x = scanner.nextInt();
int y = scanner.nextInt();
double r = scanner.nextDouble();
System.out.print("점 입력>>");
double a=scanner.nextInt();
double b=scanner.nextInt();
if((x-a)*(x-a)+(y-b)*(y-b)<r*r)
{
System.out.println("점 ("+a+","+b+")는 원 안에 있다");
}
else
System.out.println("원 안에 존재하지 않습니다.");
scanner.close();
}
}
cs
CLOSE
10. 원의 정보를 받기 위해 키보드로부터 원의 중심을 나타내는 한 점과 반지름을 입력받는다. 두 개의 원을 입력받고 두 원이 서로 겹치는지 판단하여 출력하라.
11. 숫자를 입력받아 3~5는 "봄", 6~8은 "여름", 9~11은 "가을", 12,0,1의 경우 "겨울"을, 그 외 숫자를 입력한 경우 "잘못입력"을 출력하는 프로그램을 작성하라.
if-else close
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Scanner;
public class _11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("달을 입력하세요(1~12)>>");
int month = scanner.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("잘못입력");
scanner.close();
}
}
cs
close
12.
if-else close
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Scanner ;
public class _13 {
public static void main(String [] args) {
Scanner scanner = new Scanner (System .in );
System .out .print ("연산>>" );
int x= scanner.nextInt();
String a= scanner.next();
int y= scanner.nextInt();
if (a.equals ("+" ))
{
System .out .println (x+ a+ y+ "의 연산 결과는 " + (x+ y));
}
else if (a.equals ("-" ))
{
System .out .println (x+ a+ y+ "의 연산 결과는 " + (x- y));
}
else if (a.equals ("*" ))
{
System .out .println (x+ a+ y+ "의 연산 결과는 " + (x* y));
}
else if (a.equals ("/" ))
System .out .println (x+ a+ y+ "의 연산 결과는 " + (x/ y));
scanner.close();
}
}
cs
close
switch close
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Scanner ;
public class _12_1 {
public static void main(String [] args) {
Scanner scanner = new Scanner (System .in );
System .out .print ("연산>>" );
int x = scanner.nextInt();
String s = scanner.next();
int y = scanner.nextInt();
switch (s)
{
case "+" :
System .out .println (x+ s+ y+ "의 연산 결과는 " + (x+ y));
break ;
case "-" :
System .out .println (x+ s+ y+ "의 연산 결과는 " + (x- y));
break ;
case "*" :
System .out .println (x+ s+ y+ "의 연산 결과는 " + (x* y));
break ;
case "/" :
System .out .println (x+ s+ y+ "의 연산 결과는 " + (x/ y));
break ;
}
scanner.close();
}
}
cs
close