public class Practice01 {
public static void main(String[] args) {
int x = 2;
int y = 5;
char c = 'A';
System.out.println(1 + x << 33); //6
//(1+x) << 33 -> 3 << 33
// 3 * 2^32 * 2^1 = 3*2 = 6
System.out.println(y >= 5 || x < 0 && x > 2); //true
// 1. && 연산: false && false => false
// 2. || 연산: true || false => true
System.out.println(y += 10 - x++); //13
// 1. x++ : 후위 연산, 2 대입 후 계산 (x=3으로 증가)
// 2. y += 10-2 -> y += 8 -> y = y+8 = 13
System.out.println(x += 2); //5
// 위에서 x=3 으로 증가함
// x = x + 2 = 3 + 2 = 5
System.out.println(!('A' <= c && c <= 'Z')); //false
// 1. c는 'A'로 'A'와 'Z'사이에 있음
// 2. !(true) = false
System.out.println('C' - c); //2
//1. 'C' - 'A' (int - int)
//2. 아스키값 2차이
System.out.println('5' - '0'); //5
//1. '5' - '0' (int - int)
//2. 아스키값 5차이
System.out.println(c + 1); //66
//1. 'A' + 1 -> char + int -> int + int
//2. 'A'의 아스키값 65 + 1 = 66
System.out.println(++c); //B
//1. 전위, 'A'에서 1증가
//2. 증감연산자는 형변환 일어나지 않음
System.out.println(c++); //B
//1. 후위, 우선 대입후 나중에 증가
//2. B로 출력된 후 c='C'
System.out.println(c); //C
//위에서 증가한대로 C 출력
public class Practice02 {
public static void main(String[] args) {
int numOfApples = 123;
int sizeOfBucket = 10;
//내 풀이
int numOfBucket = numOfApples % sizeOfBucket > 0 ? numOfApples / sizeOfBucket + 1 : numOfApples / sizeOfBucket;
System.out.println("필요한 바구니의 수 : " + numOfBucket);
//선생님 풀이
//나머지 == 0 -> apples/bucket + 0
//나머지 != 0 -> apples/bucket + 1
int numOfBucket = numOfapples / sizeOfBucket + ((numOfapples % sizeOfBucket > 0) ? 1 : 0 )
}
}
public class Practice03 {
public static void main(String[] args) {
int num = 10;
System.out.println(num > 0 ? "양수" : num = 0 ? "0" : "음수");
}
}
public class Practice04 {
public static void main(String[] args) {
int num = 456;
System.out.println((int)(num/100f)*100);
//내풀이
//1. 소수점화 num / 100f
//2. int로 (int)(num/100f)
//3. 자릿수복귀 (int)(num/100f)*100
System.out.println(num - num%100);
//쉬운 풀이1(정석)
System.out.println((num/100)*100);
//쉬운 풀이2
//1. 어짜피 원하는 자릿수 이하의 값은 필요 없다.
//2. num / 100 --- 4
//3. (num / 100) * 100 --- 400
}
}
public class Practice05 {
public static void main(String[] args) {
int num = 333;
System.out.println((num/10)*10+1);
//내 풀이
//1. num/10 --- 33
//2. (num/10)*10 --- 330
//3. (num/10)*10+1 --- 331
}
}
public class Practice06 {
public static void main(String[] args) {
int num = 24;
System.out.println((num/10 + 1) * 10 - num);
//내풀이
//1. (num의 값보다 크면서도 가장 가까운 10의 배수) - num
System.out.println(10 - num%10);
//쉬운 풀이
//문제: 10 - num의 1의자리
}
}
public class Practice07 {
public static void main(String[] args) {
int fahrenheit = 100;
float celcius = (int)((5f/9 * (fahrenheit - 32))*100+0.5) /100f;
//내 풀이
//1. 실수값 나오도록 공식수정 5f / 9 * (F-32)
//2. 둘째자리까지 정수화 (5f/9*(F-32))*100
//3. 반올림 (5f/9*(F-32))*100+0.5
//4. 0이하 절삭 (int)((5f/9*(F-32))*100+0.5)
//5. 자릿수복귀 (int)((5f/9*(F-32))*100+0.5)/100f
System.out.println("Fahrenheit : " + fahrenheit);
System.out.println("Celcius : " + celcius);
}
}
class Exercise3_8 {
public static void main(String[] args) {
byte a = 10;
byte b = 20;
byte c = a + b;
char ch = 'A';
ch = ch + 2;
float f = 3 / 2;
long l = 3000 * 3000 * 3000;
float f2 = 0.1f;
double d = 0.1;
boolean result = d == f2;
System.out.println("c=" + c); //c=30
System.out.println("ch=" + ch); //ch=C
System.out.println("f=" + f); //f=1.5
System.out.println("l=" + l); //l=27000000000
System.out.println("result=" + result); //result=true
}
}
수정한 코드
class Exercise3_8 {
public static void main(String[] args) {
byte a = 10;
byte b = 20;
byte c = (byte)(a + b);
char ch = 'A';
ch = (char)(ch + 2);
float f = 3f / 2;
long l = 3000L * 3000 * 3000;
float f2 = 0.1f;
double d = 0.1;
boolean result = !(d == f2);
System.out.println("c=" + c); //c=30
System.out.println("ch=" + ch); //ch=C
System.out.println("f=" + f); //f=1.5
System.out.println("l=" + l); //l=27000000000
System.out.println("result=" + result); //result=true
}
}
public class Practice09 {
public static void main(String[] args) {
char ch = 'z';
boolean b = ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ('0' <= ch && ch <= '9');
System.out.println(b);
}
}
public class Practice10 {
public static void main(String[] args) {
char ch = 'A';
char lowerCase = 'A'<=ch && ch<='Z' ? (char)(ch+32) : ch;
System.out.println("ch : " + ch);
System.out.println("ch to lowerCase : " + lowerCase);
}
}