연산자에는 다양한것들이 있는데 대표적으로는 사칙연산이 있으며
비교값을 구해주는 비교연산자 true // false값을 구해주는 논리 연산자도 있다.
int temp = 723;
System.out.println("temp 는 짝수인가?" + (temp % 2 == 0) ); // false
System.out.println("temp 는 짝수인가?" + (temp % 2 != 1) ); // false
System.out.println("temp 는 홀수인가?" + (temp %2 == 1) ); // true
System.out.println("temp 는 홀수인가?" + (temp %2 != 0) ); // true
System.out.println("temp는 3의 배수인가?" + (temp %3 == 0) ); // true
System.out.println("temp는 4의 배수인가?" + (temp %4 == 0) ); // false
System.out.println("temp는 5의 배수인가?" + (temp %5 == 0) ); // false
/는 단순한 나누기가 아닌 몫을 구하는대 쓰이며 %는 나머지를 구하는데 쓰이는것을 조심해야하며
이를 이용하면 짝수인지 홀수인지, 어떠한수의 배수인지 아닌지를 구할 수 있었다.
논리 연산자 %%(AND) 와 ||(OR) 을 사용하면 두가지 조건을 충족하는지 아닌지를 따져볼수도 있다.
int a = 101;
// a 는 100이상 이면서 짝수인가?
System.out.println( a >= 100 ); // a는 100 이상? true
System.out.println( a %2 ==0 ); // a는 짝수인가? false
System.out.println( (a >= 100) && (a % 2 == 0) ); // true && false = false
int b = 5;
// b는 1부터 10까지 숫자 범위에 포함되어 있는가?
System.out.println( (b >= 1) && (b <= 10) ); // true && true = true
System.out.println("-----------------------------------");
// ||(OR) 연산자 : 둘 다 false 면 false, 나머지는 true(AND 의 반대)
// 또는, ~거나, ~이거나
int c = 10;
// c는 10을 초과 했거나 짝수인가?
System.out.println( (c > 10) || (c % 2 == 0)); // false || true = true
논리 부정 연산자 : !를 통해 논리값을 반대로 바꾸기도 가능
boolean bool1 = true;
boolean bool2 = !bool1;
System.out.println("bool1 : " + bool1); // true
System.out.println("bool2 : " + bool2); // false
System.out.println("-----------------------------");
Scanner sc = new Scanner(System.in);
// 정수를 하나 입력 받았을 때
// 1) 해당 정수가 1부터 100사이 값이 맞는지 확인
// 2) (반대) 1부터 100사이 값이 아닌지 확인
System.out.print("정수 입력 : ");
int input = sc.nextInt();
// 1<= input <= 100
boolean result1 = 1 <= input && input <= 100;
// (input >= 1 ) && (input <= 100)
System.out.printf("%d은/는 1 이상, 100 이하의 정수인가? : %b\n", input, result1);
// 1 이상 이면서 100 이하 <-> 1 미만 또는 100 초과
boolean result2 = (input < 1) || (input > 100);
boolean result3 = !( ( input >= 1 ) && (input <= 100) );
System.out.printf("%d은/는 1 미만 100 초과 정수인가? : %b / %b\n", input, result2, result3);
증감의 전위연산과 후위연산
증감(증가, 감소) 연산자 : ++, --
-> 피연산자(값)를 1 증가 또는 감소 시키는 연산자
int iNum1 = 10;
int iNum2 = 10;
iNum1++; // iNum1 1증가
iNum2--; // iNum2 1감소
System.out.println("iNum1 : " + iNum1);
System.out.println("iNum2 : " + iNum2);
전위 연산 : ++3, --2 연산자가 앞쪽에 배치
다른 연산자보다 먼저 증가 / 감소
int temp1 = 5;
System.out.println( ++temp1 + 5);
// ++5 + 5
// 6 + 5 == 11
System.out.println("temp1 : " + temp1); // 6
후위 연산 : 10++, 6-- 처럼 연산자가 뒤쪽에 배치
-> 다른 연산자보다 나중에 증가/감소
int temp2 = 3;
System.out.println( temp2-- + 2); //5
// 3-- + 2 == 5
// temp2 = 2; (1감소)
System.out.println("temp2 : " + temp2);
int a = 3;
int b = 5;
int c = a++ + --b;
// 3++ + --5
// c = 3++ + 4
// c = 7
// a = 4
// b = 4
//최종적으로 abc 는 각각 얼마인가?
System.out.printf("%d / %d / %d\n", a, b, c);
// 4 / 4 / 7
증감연산자는 1씩 변하는데 1 외에 다른 수치로 계산하고 싶을때는 복합연산자를 사용한다.
int a = 10;
// a를 1 증가
a++; // a = a +1 == a += 1
System.out.println("a를 1 증가 : " + a);
// a를 4 증가
a += 4;
System.out.println("a를 4 증가 : " + a) ;
// a를 10 감소
a -= 10;
System.out.println("a를 10 감소 : " + a);
// a를 3배 증가
a *= 3;
System.out.println("a를 3배 증가 : " + a);
// a를 6으로 나눴을때 몫
a /= 6;
System.out.println("a를 6으로 나눈 몫 : " + a);
// a를 2로 나눴을때 나머지
a %= 2;
System.out.println("a를 2로 나눴을때 나머지 : " + a);
삼항 연산자를 통하면 조건식의 결과가 true 일때와 false 일때의 값을 구분할 수도 있었다.
int num = 30;
// num이 30보다 크면(초과) "num은 30보다 큰 수이다."
// 아니면 "num은 30 이하의 수이다" 출력
String result = num > 30 ? "num은 30보다 큰 수이다." : "num은 30 이하의 수이다.";
// 조건식 ? 식1(true) : 식2(false)
System.out.println(result);
System.out.println("-----------------------------------");
// 입력 받은 정수가 음수인지 양수인지 구분
// 단, 0은 양수로 처리
// ex)
// 정수입력 : 4
// 양수 입니다.
Scanner sc = new Scanner(System.in);
System.out.print("정수 입력 : ");
int input = sc.nextInt();
String result2 = input >= 0 ? "양수 입니다." : "음수 입니다.";
System.out.println(result2);
연습문제 풀어보기
package edu.kh.op.practice;
import java.util.Scanner;
public class OperatorPractice {
public void practice1() {
Scanner sc = new Scanner(System.in);
System.out.print("인원 수 : ");
int input1 = sc.nextInt();
System.out.print("사탕 개수 : ");
int input2 = sc.nextInt();
System.out.println();
int result1 = input2 / input1;
int result2 = input2 % input1;
System.out.println("1인당 사탕 개수 : " + result1);
System.out.println("남는 사탕 개수 : " + result2);
}
public void practice2() {
Scanner sc = new Scanner(System.in);
System.out.print("이름 : ");
String input1 = sc.next();
System.out.print("학년(정수만) : ");
int input2 = sc.nextInt();
System.out.print("반(정수만) : ");
int input3 = sc.nextInt();
System.out.print("번호(정수만) : ");
int input4 = sc.nextInt();
System.out.print("성별(남학생/여학생) : ");
String input5 = sc.next();
System.out.print("성적(소수점 아래 둘째 자리까지) : ");
double input6 = sc.nextDouble();
System.out.printf("%d학년 %d반 %d번 %s %s의 성적은 %.2f이다", input2, input3, input4, input1, input5, input6);
}
public void practice3() {
Scanner sc = new Scanner(System.in);
System.out.print("국어 : ");
int input1 = sc.nextInt();
System.out.print("영어 : ");
int input2 = sc.nextInt();
System.out.print("수학 : ");
int input3 = sc.nextInt();
int result1 = input1 + input2 + input3;
double result2 = result1 / 3;
System.out.printf("합계 : %d\n", result1);
System.out.printf("평균 : %.1f", result2);
}
}
package edu.kh.op.practice;
public class PracticeRun {
public static void main(String[] args) {
OperatorPractice pt = new OperatorPractice();
pt.practice1();
pt.practice2();
pt.practice3();