3월 ~ 5월) 자바/숙쩨
3Day - 타입변환/ 연산자와 연산식
첼로그
2023. 4. 1. 18:03
숙제 ~
https://github.com/pangocj/java/commit/18dbf75f3e0921b7079ccddb1f0dd33cd359a68d
3Day · pangocj/java@18dbf75
itwill authored and itwill committed Mar 30, 2023
github.com
변수에 저장해서 이런식으로 풀기
ex) 가로의 길이가 7이고 세로의 길이가 10인 사각형의 넓이를 계산하여 출력하세요.
int garo = 7;
int sero = 10;
int nulbe = garo * sero;
sout ("사각형의 넓이 = " + nulbe)
// 가로의 길이가 7이고 세로의 길이가 10인 사각형의 넓이를 계산하여 출력하세요.
int garo = 7;
int sero = 10;
int nulbe = garo * sero;
System.out.println("사각형 넓이 : " + nulbe);
사각형 넓이 : 70
// 높이가 9이고 밑변의 길이가 7인 삼각형의 넓이를 계산하여 출력하세요.
int Height = 9;
int length = 7;
int triangle = Height * length / 2;
System.out.println("삼각형 넓이 : " + triangle);
삼각형 넓이 : 31
// 10명의 전체 몸무게가 759Kg인 경우 평균 몸무게를 계산하여 출력하세요.
int People = 759;
int Average1 = People /10;
System.out.println(Average1);
75
// 이름이 [홍길동]인 학생이 국어점수 89점, 영어점수 93점, 수학점수 95점을 받은 경우
// 총점과 평균을 계산하여 이름, 총점, 평균을 출력하세요.
// 단, 평균은 소숫점 한자리까지만 출력하고 나머지는 절삭 처리 하세요.
String name = "Hong gil dong";
int kor = 89;
int eng = 93;
int math = 95;
int sum = kor + eng + math;
int Average2 = sum / 3;
System.out.println("이름 : "+ name);
System.out.println("총점 : "+ sum);
System.out.println("평균 : "+ math);
}
}
이름 : Hong gil dong
총점 : 277
평균 : 95
일시분초 공식 구하는법을 모르겠군!!!
// 245678초를 일/시/분/초 형식으로 변환하여 출력하세요.
// int cho = 245678;
//
// // 근데 공식을 모름..ㅋㅋ; ()
// int day = cho / (60 * 60 * 24);
// int hour = (cho - day * 60 * 60 * 24 ) / (60 * 60);
// int min = (cho - day * 60 * 60 * 24 - hour * 3600) / 60;
//
//
// System.out.println(cho + "초");
// System.out.println("일 / 시 / 분 / 초 형식으로 변환");
// System.out.println(day + " 일");
// System.out.println( hour + " 시");
// System.out.println( min + " 분");
// System.out.println( cho + " 초");
/*
245678 초 를
일 / 시 / 분 / 초 형식으로 변환
2 일
20 시
14 분
245678 초
*/
------------------------------------------------------------------------------------------
int cho = 245678;
System.out.println(cho + "초 = ");
int day = cho / (24 * 60 * 60); // 24 * 60 * 60 = 86400
cho %= 86400; // cho = cho % 86400;
int hour = cho / (60*60); // 60*60 = 3600
cho %= 3600;
int min = cho / 60;
int sec = cho % 60;
System.out.println(day + " 일 " + hour + " 시 " + min + "분" + sec + "초");
245678초 =
2 일 20 시 14분38초
// 한대의 가격이 1억 5천원만인 비행기를 20대 구매할 경우 지불해야될 금액을 계산하여 출력하세요.
//단, 15대 이상 구매할 경우 1대당 25%의 할인율을 적용하여 계산하세요.
// int plane = 150_000_000;
// // 30개 구매
// long planes = plane * 30L; // 형변환 헷갈렸음
//
// // 15개 구매 후 1대당 25% 할인
// System.out.println( " 비행기 20대 구매 가격 : " + planes + " 원");
// 비행기 20대 구매 가격 : 4500000000 원
---------------------------------------------------------------------------------------
int plane = 150_000_000;
int cnt = 20;
long money = (long) (cnt >= 15? plane * 0.75: plane) * cnt;
// (형변환) <삼항연산자 = 조건문 가능.> (cnt가 15보다 같거나 클때 ? 참인경우 * 0.75/ 거짓인경우 그냥 그대로인 가격) *cnt
System.out.println("지불 금액 = " + money);
지불 금액 = 2250000000