728x90
반응형
SMALL
연산자 실습1
: 더하기, 빼기, 곱하기, 나누기(몫) 출력
import java.util.Scanner;
public class Ex01_연산자실습1 {
public static void main(String[] args) {
//더하기, 빼기, 곱하기, 나누기(몫) 출력
Scanner sc = new Scanner(System.in);
System.out.print("첫 번째 정수 입력 : ");
int num1 = sc.nextInt();
System.out.print("두 번째 정수 입력 : ");
int num2 = sc.nextInt();
System.out.println("두 수의 더하기 : " + (num1+num2));
System.out.println("두 수의 빼기 : " + (num1-num2));
System.out.println("두 수의 곱하기 : " + (num1*num2));
System.out.println("두 수의 나누기(몫) : " + (num1/num2));
sc.close();
}
}
연산자 실습2
public class Ex02_연산자실습2 {
public static void main(String[] args) {
//원하는 줄 복사 : ctrl + alt + 방향키(위,아래)
//원하는 줄 이동 : alt + 방향키(위,아래)
//원하는 줄 삭제 : ctrl + D
//논리연산자 (NOT, AND, OR)
//: boolean(참/거짓)
//NOT 논리연산자
System.out.println(!true);
System.out.println(!false);
int a = 3;
int b = 10;
System.out.println(a>b);
//!는 부정(반대)를 의미함
System.out.println(!(a>b));
//참 : 1, 거짓 : 0
//AND 연산자(&&)
// 참 && 참 = 참
// 참 && 거짓 = 거짓
// 거짓 && 참 = 거짓
// 거짓 && 거짓 = 거짓
//OR 연산자(||)
//참 || 참 = 참
//참 || 거짓 = 참
//거짓 || 참 = 참
//거짓 || 거짓 = 거짓
}
}
연산자실습3
public class Ex03_연산자실습3 {
public static void main(String[] args) {
System.out.println((1 < 3) && (4 < 5)); //true
System.out.println((2 < 1) && (4 < 5)); //false
System.out.println((1 < 3) || (4 < 2)); //true
System.out.println((2 < 1) || (4 < 2)); //false
}
}
연산자실습4
정수 입력받아 num -> scanner 도구 사용하여 홀수, 짝수 판별하기
import java.util.Scanner;
public class Ex05_연산자실습5 {
public static void main(String[] args) {
// 정수 입력받기
Scanner sc = new Scanner(System.in);
System.out.print("정수입력>>");
int num = sc.nextInt();
//홀수인지 짝수인지 출력하기!
System.out.println((num%2==0)?"짝수":"홀수");
String result = num%2==0?"짝수":"홀수";
System.out.println(result);
sc.close();
}
}
삼항 연산자란?
: 간단한 제어 처리
? :
(삼항연산자, 조건연산자)
<구조>
(조건문) ? (실행문1) : (실행문2)
연산자실습5
두 정수 입력받아(num1, num2) 큰 수에서 작은 수를 뺀 결과값
import java.util.Scanner;
public class Ex06_연산자실습6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//두 정수 입력 받기
System.out.print("첫 번째 수>>");
int num1 = sc.nextInt();
System.out.print("두 번째 수>>");
int num2 = sc.nextInt();
//큰 수에서 작은 수 뺀 값 출력하기
System.out.println((num1>num2)? (num1-num2) : (num2-num1) );
}
}
연산자 우선순위 | |
증감연산자 | ++ -- |
산술연산자 | + - * / % |
시프트연산자 | << >> >>> |
비교연산자 | > >= < <= == != |
비트연산자 | & ^ | |
논리연산자 | && || ! |
삼항연산자 | ? : |
728x90
반응형
LIST
'스마트인재개발원 > 자바' 카테고리의 다른 글
[자바]조건문(다중if문/switch문) (0) | 2022.09.08 |
---|---|
[자바]조건문( 단순if문 / if-else문) (0) | 2022.09.08 |
[자바]대입연산자/증감연산자/비교연산자 (0) | 2022.09.07 |
[eclipse] 이클립스 자바/웹 설치방법 (0) | 2022.09.07 |
[자바] 연산자 정의/종류/산술연산자 예제 (0) | 2022.09.07 |