JAVA - 반올림, 나머지, 문자열 비교

jodbsgh·2022년 3월 14일
0

💡"JAVA"

목록 보기
3/67

Math.round()

실수를 소수점 첫 째자리에서 반올림한 정수를 반환

long result = Math.round(4.52);	//result에 5가 저장된다.

나머지 연산자 %

오른쪽 피연산자로 나누고 남은 나머지를 반환

class Ex{
	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);
    }
}

System.out.printf(10 % 8); //10을 8로 나눈 나머지 2가 출력된다.
System.out.printf(10 % -8); //위와 같은 결과를 얻는다.

문자열의 비교

문자열 비교에는 ==대신 equals()를 사용해야 한다.

String str1 = "abc";
String str2 = "abc";

System.out.println(str1 == str2);	//true
System.out.println(str1.equals(str2));// true

String str1 = new String("abc");
String str2 = new String("abc");

System.out.println(str1 == str2);		//false
System.out.println(str1.equals(str2));	//true
profile
어제 보다는 내일을, 내일 보다는 오늘을 🚀

0개의 댓글