java 논리,비교 연산자

limchard·2023년 10월 25일
0

java

목록 보기
20/48
// 비교(관계)연산자: >,<,>=,<=,==,!=
// 논리 연산자 : &&(and), ||(or), !(not)

// 결과값을 true,false로 출력해보자.

int a=5, b=3, c=5;

System.out.println(a>b); //true
System.out.println(a==b); //false
System.out.println(!(a==b)); // true
System.out.println(a!=b); // true

System.out.println(a>b&&b>c); // true&&false=false
System.out.println(a>b||b>c); // true||false=true

논리, 비교 연산자를 확인해 보자.
기본적으로 비교 연산자는 동일하다.
여기서 주의할점은 코딩의 경우 '=='가 equal의 의미이고, '='은 대입한다는 의미이다.
이점에 주의하자.
또한 같지 않다는 '!=' 이다.

논리연산자의 경우는 그냥.. 외우자.

String word;
		
		System.out.println("영어단어를 입력하세요.");
		System.out.println("입력예) happy,apple,angel,rose,cat,food");
		word=sc.nextLine();
		
		System.out.println("입력한 문자열 : "+word);
		
		if(word.equals("angel"))	// equals : 대소문자 모두 따질때 // equalsIgnoreCase : 대소문자 상관없이 단어만 볼때 사용 		
			System.out.println("***천사***");
		else if(word.equals("happy"))
			System.out.println("**행복하다**");
		else if(word.equals("rose"))
			System.out.println("**장미**");
		else if (word.equals("cat"))
			System.out.println("**고양이**");
		else
			System.out.println("**한글단어가 등록됩지 않았습니다.");

equals라는 소스코드이다.
주로 문자를 비교할때 사용한다.
if문 등등 조건을 줄때 문자관련 비교 및 조건을 줄때 사용된다.

profile
java를 잡아...... 하... 이게 맞나...

0개의 댓글