본문 바로가기
공부/개발노트

[이것이 자바다] 문제 3번 답안

반응형


public class Q1_Q1 {
public static void main(String[] args) {
// Exercise02.java 후행연산자 선행연산자 이해
int x = 13;
int y = 20;
int z = (++x) + (y--);
System.out.println(z);
// Exercise03.java 삼항연산자 이해
int score = 85;
String result = (!(score > 90))? "가" : "나";
System.out.println(result);
// Exercise04.java 남은 연필 수와 나눠가질 수 있는 연필수
int penciles = 534;
int student = 30;
/* 학생 한 명이 가지는 연필 수 */
int pencilsPerstudent = (penciles / student);
System.out.println("학생 당 연필 갯수는 " + pencilsPerstudent + " 자루");
/* 남은 연필 수 */
int pencilsLeft = (penciles % student);
System.out.println("나눠주고 남은 연필 갯수는 " + pencilsLeft + " 자루");
// Exercise05.java 산술연산자
int Value = 356;
System.out.println(Value / 100 * 100);
// Exercise06.java 사다리꼴 계산
int lenthTop = 5;
int lenthBottom = 10;
int heigh = 7;
double area = ((lenthTop + lenthBottom)* heigh)*0.5;
System.out.println("사다리꼴 넓이: " + area);
// Exercise07.java 논리연산자
int xx = 10;
int yy = 5;
System.out.println((xx>7) && (yy <= 5)); // 출력 결과: true && true = true
System.out.println((xx%3 == 2) || (yy%2 != 1)); // 출력 결과: false && false =
// Exercise08.java NaN 검사
double xxx = 5.0;
double yyy = 0.0;
double zzz = xxx % yyy;
if(Double.isNaN(zzz)) { // 나눌 수 없다
System.out.println("0.0으로 나눌 수 없는 값입니다.");
} else { // 나눌 수 있다
double result01 = zzz +10;
System.out.println("결과: " + result01);
}
}
}
view raw Question03.java hosted with ❤ by GitHub


[실행 결과]





반응형