cf. 자바 단축키
- sysout + ctrl + 스페이스바-> System.out.println 자동 생성
- shift + alt + n +c -> 클래스 자동 생성
1. 환율
import java.util.Scanner;
public class ex_1 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("원화를 입력하세요(단위 원) : ");
int won=scan.nextInt();
double dollar;
dollar=won/1100;
System.out.println(won+"원은 $"+dollar+"입니다.");
scan.close();
}
}
2. /와 %연산자
import java.util.Scanner;
public class ex2 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("정수를 입력하세요(0-99) : ");
int num=scanner.nextInt();
if((num/10)==(num%10))
System.out.println("Yes! 10의 자리와 1의 자리가 같습니다.");
else
System.out.println("No!");
scanner.close();
}
}
3. 거스름돈
import java.util.Scanner;
public class ex_3 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("금액을 입력하시오 : ");
int m=s.nextInt();
System.out.println("오만원 권 "+ m/50000 +"매");
m%=50000;
System.out.println("만원 권 "+ m/10000 +"매");
m%=10000;
System.out.println("천원 권 "+ m/1000 +"매");
m%=1000;
System.out.println("백원 "+ m/100 +"매");
m%=100;
System.out.println("오십원 "+ m/50 +"매");
m%=50;
System.out.println("십원 "+ m/10 +"매");
m%=10;
System.out.println("일원 "+ m +"매");
s.close();
}
}
4. 중간 값 구하기
import java.util.Scanner;
public class Q4 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("정수 3개 입력>>");
int n1=s.nextInt();
int n2=s.nextInt();
int n3=s.nextInt();
System.out.print("중간 값은 ");
if(n1>n2) {
if(n1>n3) {
System.out.println((n2>n3)?n2:n3);
}else {
System.out.println((n1>n2)?n1:n2);
}
}else {
if(n2>n3) {
System.out.println((n1>n3)?n1:n3);
}else {
System.out.println((n1>n2?n1:n2));
}
}
s.close();
}
}
5. 삼각형의 3변의 길이를 보고 삼각형이 될까, 안 될까.
import java.util.Scanner;
public class ex5 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("정수 3개를 입력하시오 : ");
int n1=scan.nextInt();
int n2=scan.nextInt();
int n3=scan.nextInt();
boolean tri;
if(n1>=n2) {
if(n1>=n3)
tri=(n1*n1==n2*n2+n3*n3);
else
tri=(n3*n3==n2*n2+n1*n1);
}
else {
if(n2>=n3)
tri=(n2*n2==n1*n1+n3*n3);
else
tri=(n3*n3==n2*n2+n1*n1);
}
if (tri)
System.out.println("삼각형이 됩니다");
else
System.out.println("삼각형이 안 됩니다.");
scan.close();
}
}
cf. 같은 문제 교수님이 풀어주신 거.. 삼각형이 되려면 두 변의 합이 다른 한 변의 합보다 커야한다.
import java.util.Scanner;
public class Eccc {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("정수 3개를 입력하세요 : ");
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
if((a+b>c)&&(b+c>a)&&(c+a>b))
System.out.println("삼각형이 됩니다.");
else
System.out.println("안 됩니다.");
s.close();
}
}
6. 369게임
import java.util.Scanner;
public class ex_6 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("1-99사이의 정수를 입력하세요 : ");
int num=s.nextInt();
if((num/10)%3==0 && (num%10)%3==0)
System.out.println("박수짝짝");
else if((num/10)%3==0 || (num%10)%3==0)
System.out.println("박수짝");
else
System.out.println(num);
s.close();
}
}
<추가> 위에 코드의 문제는 10자리나 1의 자리가 0일 때도 첫번째 조건문에 들어간다는 거다.
1) 일단 박수 짝짝이려면 1의 자리 10의 자리 모두 369중에 하나여야 하므로, 두 자리 모두 0이 아니고 3으로 나누어 떨어져야 한다.
2) 두번째 조건은 10의 자리나 1의 자리가 3으로 나누어 떨어지는데 이 때 1의 자리는 0이면 안 된다.
import java.util.Scanner;
public class Q6 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("1-99사이의 정수를 입력>>");
int num=s.nextInt();
int n10=num/10, n1=num%10;
if((n10!=0) && (n10%3==0) && (n1%3==0) && n1!=0) {
System.out.println("박수 짝짝");
}else if((n10%3==0) || (n1%3==0) && n1!=0) {
System.out.println("박수 짝");
}
s.close();
}
}
7. 사각형 안에 점이 있을까
import java.util.Scanner;
public class ex_7 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int x, y;
System.out.println("점 (x,y)의 좌표를 입력하시오 : ");
x=s.nextInt();
y=s.nextInt();
if(x>=100 && x<=200) {
if(y>=100 && y<=200)
System.out.println("("+x+", "+y+")는 사각형 안에 있습니다.");
}
else System.out.println("사각형 안에 없습니다.");
s.close();
}
}
8. 직사각형과 충돌하는가?
import java.util.Scanner;
public class ex_8 {
public static boolean inRect(int x1, int y1, int x2, int y2) {
if ((x1>=100 || x2<=200) && (y1>=100 || y2<=200)){
return true;
}else return false;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int x1,x2,y1,y2;
System.out.println("두 점을 입력하세요 : ");
x1=s.nextInt();
y1=s.nextInt();
x2=s.nextInt();
y2=s.nextInt();
if (inRect(x1,y1,x2,y2)){
System.out.println("충돌합니다.");
}else {
System.out.println("충돌 안 합니다.");
}
s.close();
}
}
9. 점이 원의 내부에 있는가?
import java.util.Scanner;
import java.lang.Math;
public class ex9 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
double x, y, radius;
double x2,y2;
System.out.println("원의 중심과 반지름 입력 : ");
x=scan.nextDouble();
y=scan.nextDouble();
radius=scan.nextDouble();
System.out.println("점 입력 : ");
x2=scan.nextDouble();
y2=scan.nextDouble();
double distance= Math.sqrt(Math.pow((x-x2),2)+Math.pow((y-y2),2));
System.out.print("점 (" + x2 + ", " + y2 +")는 ");
if(distance<radius)
System.out.println("원 안에 있다." );
else if(distance>radius)
System.out.println("원 안에 없다." );
else
System.out.println("원 위에 있다." );
scan.close();
}
}
cf. 점과 원의 중심의 거리<반지름 : 원 안에 있음
cf. 필요한 함수
- Math.pow(제곱할 값, 몇 제곱) : 제곱
- Math.sqrt() : 제곱근
10. 두 원이 겹치는가?
import java.util.Scanner;
public class ex_10 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int x1, y1, r1, x2, y2, r2;
double d;
System.out.println("첫번째 원의 중심과 반지름 : ");
x1=s.nextInt();
y1=s.nextInt();
r1=s.nextInt();
System.out.println("두번째 원의 중심과 반지름 : ");
x2=s.nextInt();
y2=s.nextInt();
r2=s.nextInt();
d= Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1), 2));
if(r2-r1<d && r2+r1>d) {
System.out.println("두 원은 서로 겹친다.");
}else {
System.out.println("두 원은 서로 겹치지 않는다.");
}
s.close();
}
}
cf. 큰 원의 반지름-작은 원의 반지름 < 두 점 사이의 거리 < 큰 원의 반지름+작은 원의 반지름: 두 원은 겹친다
11. 달을 적으면 계절을 알려주는 프로그램
아 여기서 스캐너 안 닫아 줬네
<if문>
import java.util.Scanner;
public class ex_11 {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.print("달을 입력하세요(1-12) : ");
int month=s.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("잘못 입력했습니다.");
}
}
<swith문>
import java.util.Scanner;
public class ex_11switch {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("달을 입력하세요(1-12) : ");
int month=s.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;
}
}
}
12. 사칙연산 프로그램
<if문>
import java.util.Scanner;
public class ex_12 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("연산>> ");
double num1=s.nextDouble();
String ope=s.next();
double num2=s.nextDouble();
System.out.print(num1 + ope + num2 + "의 계산 결과는 ");
if(ope.equals("+"))
System.out.println(num1+num2);
else if(ope.equals("-"))
System.out.println(num1-num2);
else if(ope.equals("*"))
System.out.println(num1*num2);
else {
if(num1==0 || num2==0);
System.out.println("0으로 나눌 수 없습니다.");
System.out.println(num1/num2);
}
s.close();
}
}
<switch>
import java.util.Scanner;
public class ex_12_2 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("연산>> ");
double num1=s.nextDouble();
String ope=s.next();
double num2=s.nextDouble();
switch(ope) {
case "+":
System.out.println(num1+num2);
break;
case "-":
System.out.println(num1-num2);
break;
case "*":
System.out.println(num1*num2);
break;
case "/":
System.out.println(num1/num2);
}
s.close();
}
}
'JAVA > 명품 자바 프로그래밍' 카테고리의 다른 글
자바 기초 문법 정리- 6장 모듈과 패키지 (0) | 2021.10.05 |
---|---|
자바 기초 문법 정리- 4~5장 클래스~상속 (0) | 2021.09.30 |
[3장] 도전 문제 (0) | 2021.04.15 |
[교수님 퀴즈] 자바 2-3장 (0) | 2021.04.09 |
[명품 자바 프로그래밍] 3장 실습 문제 (0) | 2021.04.07 |