큐범
Just do debug
큐범
전체 방문자
오늘
어제
  • 전체보기 (128)
    • 회고 (4)
    • JAVA (16)
      • JAVA 기초 (18)
      • JAVA Algorithm, Datastruct (13)
    • Spring (11)
    • Micro Service Architecture (3)
    • JPA (6)
    • gRPC (4)
    • Network (8)
    • Process (7)
    • Cloud (4)
    • Python (10)
    • Web(vue) (2)
    • UMC (1)
    • DB (9)
    • CS (1)
    • Clean Code (1)
    • TDD (9)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

인기 글

최근 댓글

최근 글

hELLO · Designed By 정상우.
큐범

Just do debug

JAVA/JAVA 기초

[JAVA 기초]산술 연산자

2021. 7. 8. 17:12

사칙 연산자 (+ - * /)

사칙연산자는 덧셈(+), 뺄셈(-), 곱셈(*), 나눗셈(/), 나머지(%)이 있다.

주의 할 점 : int/int는 결과 값이 float이나 double이 아닌 int인 것을 명심해야 한다.

public static void main(String[] args) {
		int a = 10;
		int b = 4;
		
		System.out.printf("%d + %d = %d%n", a,b,a+b);
		System.out.printf("%d - %d = %d%n", a,b,a-b);
		System.out.printf("%d * %d = %d%n", a,b,a*b);
		System.out.printf("%d / %d = %d%n", a,b,a/b);
		System.out.printf("%d / %f = %f%n", a,(float)b, a/(float)b);
	}
//결과
10 + 4 = 14
10 - 4 = 6
10 * 4 = 40
10 / 4 = 2
10 / 4.000000 = 2.500000

나머지 연산자 %

public static void main(String[] args) {
		int x = 10;
		int y = 8;
		
		System.out.printf("%d을 %d로 나누면, %n", x, y);
		System.out.printf("몫은 %d이고, 나머지는 %d입니다. %n", x/y,x%y);

	}
//결과
10을 8로 나누면, 
몫은 1이고, 나머지는 2입니다.

 

출처 : JAVA의 정석 - (남궁성지음)

    'JAVA/JAVA 기초' 카테고리의 다른 글
    • [JAVA 기초] 논리연산자(&&, ||, !)
    • [JAVA 기초]비교 연산자
    • [JAVA 기초]연산자(operator)
    • [JAVA 기초]형변환(casting)
    큐범
    큐범

    티스토리툴바