π§© νμ΅ λ¨μ : μ°μ°μ(Operator)
π νμ΅ λμ : <Javaμ μ μ 3ν>(λ¨κΆ μ± μ )
βμ€λ΅ μ 리 μλ£(2021.12.25)
[3-1] λ€μ μ°μ°μ κ²°κ³Όλ₯Ό μ μΌμμ€. β
int x = 2;
int y = 5;
char c = 'A'; // 'A'μ λ¬Έμμ½λλ 65
System.out.println(1 + x << 33);
6
μ°μ μμμ λ§μ μ°μ°μ > μ¬ννΈ μ°μ°μμ΄λ―λ‘ 3 << 33... intλ 32 bitμ΄λ―λ‘ 33λ² μ¬ννΈ νμ§ μκ³ 1λ²λ§ μ¬ννΈ νλ€. '3 << 1'μ 3μ ‘2μ 1μ κ³±’μΈ 2λ₯Ό κ³±νλ κ²κ³Ό κ°μ κ²°κ³Όλ₯Ό μ»μΌλ―λ‘ '3 * 2'κ° λμ΄ κ²°κ΅ 6μ μ»λλ€.
System.out.println(y >= 5 || x < 0 && x > 2);
&& μ°μ°μκ° λ¨Όμ μνλμ΄μ falseμμ§λ§ yλ 5λΌμ μ΅μ’ μ μΌλ‘ true
System.out.println(y += 10 - x++);
y = y + 10 - x++;λΌλ μμ΄λ―λ‘ y = 13, x = 3μΌλ‘ λ§μ³μ§
y = y + (10 - x++);μ΄ μ ννλ€!
System.out.println(x+=2);
x = x + 2λΌλ μμ΄λκΉ 5
System.out.println( !('A' <= c && c <='Z') );
false
System.out.println('C'-c);
μμ€ν€μ½λνμμμ 'C'-'A'κ° λλ κ²μ΄λΌ 2λ§νΌ μ°¨μ΄λλ―λ‘ 2
67 - 65 = 2
System.out.println('5'-'0');
λ§μ°¬κ°μ§λ‘ μ½λνμμμ κ°μ§ μ«μκ°μ΄ 5λ§νΌ μ°¨μ΄λ κ²μ΄λ―λ‘ 5
53 - 48 = 5
System.out.println(c+1);
65 + 1 = 66
System.out.println(++c);
66
B
β μ΄νμ°μ°μλ μ°μ°μ μν΄ intνλ³΄λ€ μμ νμ λ€μ νλ³ννλ€. νμ§λ§ λ¨νμ°μ°μμΈ μ¦κ°μ°μ°μ ++μ νλ³νμ΄ λ°μνμ§ μλλ€. λλ¬Έμ μΆλ ₯λ¬Έ printlnμ λ³μ cλ₯Ό μ΄ κ°μ νμ μ λ°λΌ λ¬Έμλ‘ μΆλ ₯ν κ²μ΄λ€.
System.out.println(c++);
66
B
β μμ λ§μ°¬κ°μ§μ΄λ€. νλ³νμ΄ μ΄λ£¨μ΄μ§μ§ μλ λ¨νμ°μ°μμ΄κΈ°μ μΆλ ₯λ¬Έμ charνμ λ§μΆ° λ¬Έμ νμ μΌλ‘ κ°μ λ°ννλ€.
System.out.println(c);
67
C
β μμ λ§μ°¬κ°μ§μ΄λ€. νλ³νμ΄ μ΄λ£¨μ΄μ§μ§ μλ λ¨νμ°μ°μμ΄κΈ°μ μΆλ ₯λ¬Έμ charνμ λ§μΆ° λ¬Έμ νμ μΌλ‘ κ°μ λ°ννλ€.
[3-2] μ¬κ³Όμ μκ° 123κ°μ΄κ³ νλμ λ°κ΅¬λμλ 10κ°μ μ¬κ³Όλ₯Ό λ΄μ μ μλ€λ©΄, 13κ°μ λ°κ΅¬λκ° νμν κ²μ΄λ€. μλ§μ μ½λλ₯Ό λ£μΌμμ€. β
package com.reminder.exercises;
public class Ex01 {
public static void main(String[] args) {
int numOfApples = 123;
int sizeOfBucket = 10;
int numOfBucket = ((numOfApples / sizeOfBucket) + ((numOfApples % sizeOfBucket > 0) ? 1 : 0));
System.out.println("νμν λ°κ΅¬λμ μ : " + numOfBucket);
}
}
νμν λ°κ΅¬λμ μ : 13
[3-3] μλλ λ³μ numμ κ°μ λ°λΌ ‘μμ’, ‘μμ’, ‘0’μ μΆλ ₯νλ μ½λμ΄λ€. μΌν μ°μ°μλ₯Ό μ΄μ©ν΄μ μλ§μ μ½λλ₯Ό λ£μΌμμ€. β
package com.reminder.exercises;
public class Ex02 {
public static void main(String[] args) {
int num = 10;
System.out.println((num > 0) ? "μμ" : ((num < 0) ? "μμ" : "0"));
}
}
μμ
[3-4] λ°±μ μ리 μ΄νλ₯Ό λ²λ¦¬λ μ½λμ΄λ€. λ§μΌ λ³μ numμ κ°μ΄ ‘456’μ΄λΌλ©΄ ‘400’μ΄ λκ³ , ‘111’μ΄λΌλ©΄ ‘100’μ΄ λλ€. μλ§μ μ½λλ₯Ό λ£μΌμμ€. ββ
package com.reminder.exercises;
public class Ex03 {
public static void main(String[] args) {
int num1 = 456;
// System.out.println(((int)(num1 * 0.01)) * 100);
System.out.println(num1 / 100 * 100);
int num2 = 111;
// System.out.println(((int)(num2 * 0.01)) * 100);
System.out.println(num2 / 100 * 100);
}
}
400
100
λͺ«μ ꡬν΄μ 100 κ³±νκΈ°! λ무 μ΄λ ΅κ² μκ°νμ§ λ§μ!
[3-5] μΌμ μ리λ₯Ό 1λ‘ λ°κΎΈλ μ½λμ΄λ€. μλ§μ μ½λλ₯Ό λ£μΌμμ€. β
package com.reminder.exercises;
public class Ex04 {
public static void main(String[] args) {
int num1 = 333;
int num2 = 777;
// System.out.println(num1 / 10);
// System.out.println(num1 / num1);
System.out.println(num1 / 10 * 10 + 1);
System.out.println(num2 / 10 * 10 + 1);
}
}
// 33
// 1
331
771
[3-6] λ³μ numμ κ°λ³΄λ€ ν¬λ©΄μλ κ°μ₯ κ°κΉμ΄ 10μ λ°°μμμ λ³μ numμ κ°μ λΊ λλ¨Έμ§λ₯Ό ꡬνλ μ½λμ΄λ€. μλ§μ μ½λλ₯Ό λ£μΌμμ€. β
package com.reminder.exercises;
public class Ex05 {
public static void main(String[] args) {
int num1 = 24;
int num2 = 19;
int num3 = 81;
System.out.println(10 - (num1 % 10));
System.out.println(10 - (num2 % 10));
System.out.println(10 - (num3 % 10));
}
}
6
1
9
[3-7] νμ¨(Fahrenheit)λ₯Ό μμ¨(Celcius)λ‘ λ³ννλ μ½λμ΄λ€. λ³ν곡μμ΄ 'C = 5/9 ×(F - 32)'λΌκ³ ν λ, (1)μ μλ§μ μ½λλ₯Ό λ£μΌμμ€. λ¨, λ³ν κ²°κ³Όκ°μ μμμ μ μ§Έμ리μμ λ°μ¬λ¦Όν΄μΌ νλ€. β
package com.reminder.exercises;
public class Ex06 {
public static void main(String[] args) {
int fahrenheit = 100;
// float celcius = ((float)5/9 * (fahrenheit - 32));
float celcius = (int)(5/9f * (fahrenheit - 32) * 100 + 0.5) / 100f;
System.out.println("Fahrenheit : " + fahrenheit);
System.out.println("Celcius : " + celcius);
}
}
[3-8] μλ μ½λμ λ¬Έμ μ μ μμ ν΄μ μ€νκ²°κ³Όμ κ°μ κ²°κ³Όλ₯Ό μ»λλ‘ νμμ€. β
package com.reminder.exercises;
public class Ex07 {
public static void main(String[] args) {
byte a = 10;
byte b = 20;
byte c = (byte)(a + b);
System.out.println("c="+c);
char ch = 'A';
/* μ£Όμ΄μ§ μ€λ₯ μ½λ */
// ch = ch + 2;
/* λ³κ²½ μ½λ */
// ch += 2;
/* λ΅μ μ½λ */
ch = (char)(ch + 2);
System.out.println("ch="+ch);
float f = 3 / 2f;
long l = 3000 * 3000 * 3000L;
System.out.println("f="+f);
System.out.println("l="+l);
float f2 = 0.1f;
/* μ£Όμ΄μ§ μ½λ */
// double d = 0.1;
/* λ³κ²½ μ½λ */
double d = (float)0.1;
/* λ΅μ μ½λ */
// boolean result = (float)d==f2;
boolean result = d==f2;
System.out.println("result="+result);
}
}
c=30
ch=C
f=1.5
l=27000000000
result=true
[3-9] λ€μμ λ¬Έμν λ³μ chκ° μλ¬Έμ(λλ¬Έμ λλ μλ¬Έμ)μ΄κ±°λ μ«μμΌ λλ§ λ³μ bμ κ°μ΄ trueκ° λλλ‘ νλ μ½λμ΄λ€. μλ§μ μ½λλ₯Ό λ£μΌμμ€. β
package com.reminder.exercises;
public class Ex08 {
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);
}
}
true
[3-10] λ€μμ λλ¬Έμλ₯Ό μλ¬Έμλ‘ λ³κ²½νλ μ½λμΈλ°, λ¬Έμ chμ μ μ₯λ λ¬Έμκ° λλ¬ΈμμΈ κ²½μ°μλ§ μλ¬Έμλ‘ λ³κ²½νλ€. λ¬Έμμ½λλ μλ¬Έμκ° λλ¬Έμλ³΄λ€ 32λ§νΌ λ ν¬λ€. μλ₯Ό λ€μ΄ 'A'μ μ½λλ 65μ΄κ³ 'a'μ μ½λλ 97μ΄λ€. μλ§μ μ½λλ₯Ό λ£μΌμμ€. β
package com.reminder.exercises;
public class Ex09 {
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);
}
}
ch: A
ch to lowerCase: a
'Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μλ°μ μ μ] Ch 4. μ μ΄λ¬Έ μ°μ΅λ¬Έμ νμ΄ (0) | 2021.12.25 |
---|---|
[μλ°μ μ μ] Ch 3. μ°μ°μ μμ μμ© νμ΅ (0) | 2021.12.25 |
[JAVA] 4-3. λΆκΈ°λ¬Έ break, continue (0) | 2021.12.25 |
λ°λ³΅λ¬Έ μμ Quiz (0) | 2021.12.24 |
[JAVA] 4-2. λ°λ³΅λ¬Έ for, while (0) | 2021.12.24 |