[JAVA] 코드업 기초 100문제 - [기초-논리연산] 1053 ~ 1058

박서현·2021년 6월 11일
0

앞선 글에서는 JAVA로 비교연산에 관한 문제와 답안을 작성했다.
[JAVA] 코드업 기초 100문제 - [기초-비교연산] 1049 ~ 1052

오늘 글에서는 코드업 기초 100문제 중 논리연산에 관한 문제와 답안을 정리할 것 이다.

코드업 1053 : [기초-논리연산] 참 거짓 바꾸기

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int a = sc.nextInt();
		
		if(a==0){
		    System.out.println(1);
		}else{
		    System.out.println(0);
		}
        
        //삼항 연산자 사용
        //System.out.println(a == 1 ? 0 : 1);
      
    }
}

설명 ) 간단한 if 조건문을 사용할 때, 한 줄로 줄여서 사용할 수 있다. -> 삼항연산자

코드업 1054 : [기초-논리연산] 둘 다 참일 경우만 참 출력하기

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
		int a =sc.nextInt();
		int b =sc.nextInt();

		if(a==1 && b==1) {
			System.out.println(1);
		}else {
			System.out.println(0);
		}
        
        //삼항 연산자 사용
        //System.out.println((a==1 && b==1) ? 1 : 0);
      
    }
}

코드업 1055 : [기초-논리연산] 하나라도 참이면 참 출력하기

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
		int a =sc.nextInt();
		int b =sc.nextInt();

		if(a==1 || b==1) {
			System.out.println(1);
		}else {
			System.out.println(0);
		}
        
        //삼항 연산자 사용
        //System.out.println((a==1 || b==1) ? 1 : 0);
      
    }
}

코드업 1056 : [기초-논리연산] 참/거짓이 서로 다를 때에만 참 출력하기

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
		int a =sc.nextInt();
		int b =sc.nextInt();

		if(a!=b) {
			System.out.println(1);
		}else {
			System.out.println(0);
		}
        
        //삼항 연산자 사용
      	//System.out.println((a!=b)? 1: 0);
      
    }
}

코드업 1057 : [기초-논리연산] 참/거짓이 서로 같을 때에만 참 출력하기

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();

		if(a==b) {
			System.out.println(1);
		}else {
			System.out.println(0);
		}
        
        //삼항 연산자 사용
      	//System.out.println((a==b)? 1: 0);
    }
}

코드업 1058 : [기초-논리연산] 둘 다 거짓일 경우만 참 출력하기

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();

		if(a==0 && b==0) {
			System.out.println(1);
		}else {
			System.out.println(0);
		}
        
        //삼항 연산자 사용
      	//System.out.println((a==0 && b==0)? 1: 0);
      
    }
}
profile
불편함을 당연시 여기지 않고, 더 나은 프로세스를 위해, 더 나은 사용자 경험을 위해, 공부하고, 메모하고, 개발하는 박서현 입니다.

0개의 댓글