벌써 하기 싫은 이 마음... 하..
힘내서 아자아자 화이팅!!!!!
+
: 더하기-
: 빼기*
: 곱하기/
: 나누기 (몫)%
: 나누기 (나머지)만약 이 사칙연산자가 섞여 나와도 우리가 아는 그냥 사칙연산 순서대로 계산이 수행된다.
곱하기 = 나누기 > 더하기 = 빼기
<
>
>=
<=
==
: 같다 (이꼬르)!=
: 같지 않다'이 비교연산자들은 결과값을 Boolean이 나온다!
True
/False
비교연산의 결과값으로 받을 수 있는 Boolean
값을 연결하는 연산자
조건을 연결했을 때 Boolean
값을 연결하는 연산자
&&
: AND ➡️ 피연산자가 모두 True
이면 True
, 하나라도 False
가 있으면 False
.||
: OR ➡️ 피연산자가 하나라도 True
이면 True
.!
: NOT ➡️ 논리부정연산자. 뒤집기!!!! True
➡️ False
/ False
➡️ True
public class W02 {
public static void main(String[] args) {
boolean flag1 = true;
boolean flag2 = false;
System.out.println(!flag1);
System.out.println(!flag2);
System.out.println(!(5 == 5));
}
}
💡 결과값이
True
아니면False
이기때문에 변수명을 보통flag
라고 한다 ex)flag1
,flag2
, ...
💡 묶어야 뒤집을 수 있다!
System.out.println(1 < 3 < 5); // 안돼안돼안돼
✔️ 이렇게 3개 이상의 비교는 안돼 ! 항상 2개 비교 !
=
+=
-=
*=
/=
%=
++
(+=1
)--
(-=1
)(type)
: 형변환 연산자public class W02 {
public static void main(String[] args) {
int intNumber = 93 + (int)98.8;
System.out.println(intNumber);
double doubleNumber = (double) 93 + 98.8;
System.out.println(doubleNumber);
}
}
? :
: 삼항연산자 ➡️ 조건 ? 참 : 거짓public class W02 {
public static void main(String[] args) {
int x = 1;
int y = 9;
boolean b = (x == y) ? true : false;
System.out.println(b);
String s = (x != y) ? "정답" : "오답";
System.out.println(s);
int max = (x > y) ? x : y;
System.out.println(max);
int min = (x > y) ? y : x;
System.out.println(min);
}
}
instance of
: 피연산자가 조건에 명시된 클래스의 객체인지 비교하여 맞으면 True
, 틀리면 False
Byte = 8 bit
bit
는 0
, 1
둘중의 하나의 값만을 저장하는 컴퓨터가 저장 가능한 가장 작은 단위
컴퓨터의 가장 작은 단위인 bit
이기때문에 연산중에서 bit
연산이 제일 빠르다.
0
, 1
값으로 산술연산을 하거나 비교연산을 할 수 있지만 비트 연산을 통해 자리수를 옮길수도 있다.
bit
의 자리수를 옮기는 것을 비트 연산이라고 한다.
<<
: 왼쪽으로 자리수 옮기기>>
: 오른쪽으로 자리수 옮기기System.out.println(5 << 3); // 40
System.out.println(8 << 2); // 32
System.out.println(22 >> 3); // 2
System.out.println(123 >> 5); // 3
연산자 여러개가 함께 있는 연산을 계산할 때는 우선순위가 있다
아래 우선순위에 따라서 최종적인 응답값이 결정된다.
* 단, 괄호로 감싸주면 활호안의 연산이 최우선순위로 계산된다
산술 > 비교 > 논리 > 대입
연산 전에 두 피연산자의 타입이 다른 경우 타입을 일치시킨다.
* 둘중에 저장공간 크기가 더 큰 타입으로 일치시킨다.
1. 피연산자의 타입이 int
보다 작은 short
타입이면 int
로 변환
2. 피연산자의 타입이 long
보다 작은 int
, short
타입이면 long
로 변환
3. 피연산자의 타입이 float
보다 작은 int
, short
, long
타입이면 float
로 변환
4. 피연산자의 타입이 double
보다 작은 int
, short
, long
, float
타입이면 double
로 변환
∴ 변수 여러 개를 연산했을 때 결과값은 피연산자 중 표현 범위가 가장 큰 변수 타입을 가지게 된다.
else if는 많이 쓸 수 있다!
if (조건) {
~~~~~~
} else if (조건) {
~~~~~
} else {
~~~
}
두 개의 값을 비교하는 메소드
Objects.equals(A, B)
: A, B가 같은 경우true
, 다른 경우false
import java.util.Objects;
import java.util.Scanner;
public class W02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// A에게 값 입력받기
System.out.println("A 입력 : ");
String aHand = sc.nextLine();
// B에게 값 입력받기
System.out.println("B 입력 : ");
String bHand = sc.nextLine();
if (Objects.equals(aHand, "가위")){
if (Objects.equals(bHand, "가위")){
System.out.println("비겼습니다.");
} else if (Objects.equals(bHand, "바위")){
System.out.println("B가 이겼습니다.");
} else if (Objects.equals(bHand, "보")){
System.out.println("A가 이겼습니다.");
} else {
System.out.println("B가 이상한 값을 입력했습니다.");
}
} else if (Objects.equals(aHand, "바위")){
if (Objects.equals(bHand, "가위")){
System.out.println("A가 이겼습니다.");
} else if (Objects.equals(bHand, "바위")){
System.out.println("비겼습니다.");
} else if (Objects.equals(bHand, "보")){
System.out.println("B가 이겼습니다.");
} else {
System.out.println("B가 이상한 값을 입력했습니다.");
}
} else if (Objects.equals(aHand, "보")) {
if (Objects.equals(bHand, "가위")){
System.out.println("B가 이겼습니다.");
} else if (Objects.equals(bHand, "바위")){
System.out.println("A가 이겼습니다.");
} else if (Objects.equals(bHand, "보")){
System.out.println("비겼습니다.");
} else {
System.out.println("B가 이상한 값을 입력했습니다.");
}
} else {
System.out.println("A가 이상한 값을 입력했습니다.");
}
}
}
switch (month) {
case 1: monthString = "1월";
break;
case 2: monthString = "2월";
break;
default: monthString = "알 수 없음";
}
case
문에는 break
를 꼭 넣어야한다.default
를 꼭 넣어야한다.if
문보다 훨씬 가독성이 좋다. 특정 조건에 따라 연산을 반복해서 수행하고 싶을 때 사용하는 문맥
for (초기값 ; 조건 ; 증감문) {
}
public class W03 {
public static void main(String[] args) {
// 구구단 만들기 !!
for (int i = 2; i < 10; i++){
System.out.println("\n" + i + " 단");
for (int j = 1; j < 10; j++){
System.out.println(i + " x " + j + " = " + i*j);
}
}
}
}
int[] numbers = {3, 6, 9 ,12, 15};
for (int number : numbers) {
}
while (조건) {
증감문;
}
do 안에 있는 로직을 먼저 수행한다.
do {
} while (조건)
break
: 가장 가까운 블록의 for
문 또는 while
, switch
문을 중단!
continue
: pass하고 다음 코드 수행
public class W03 {
public static void main(String[] args) {
//입력받는 단을 제외하고 출력!!
Scanner sc = new Scanner(System.in);
int dan = sc.nextInt();
// 구구단 만들기 !!
for (int i = 2; i < 10; i++){
System.out.println("\n" + i + " 단");
if (i == dan){
continue;
}
for (int j = 1; j < 10; j++){
System.out.println(i + " x " + j + " = " + i*j);
}
}
}
}
public class W03 {
public static void main(String[] args) {
//입력받은 단만 출력!!
Scanner sc = new Scanner(System.in);
int dan = sc.nextInt();
for (int i = 2; i < 10; i++){
if(i == dan) {
for (int j = 1; j < 10; j++){
System.out.println(i + " x " + j + " = " + i*j);
}
}
}
}
}