메뉴 건너뛰기

HEUKMYO

program1

140618_[java]논리연산자

2014.06.18 15:08 read.31

논리연산자

> 왼쪽이 크다
>= 왼쪽이 크거나 같다
< 오른쪽이크다
<= 오른쪽이 크거나 같다
== 같다
!= 같지 않다
|| 둘중에 하나가 참일때 참
&& 둘다 참일때 참



예제1

a가 b보다 작을 경우 hellow를 출력해라 아니면 world를 출력해라

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		int a=10;
		int b=20;
		
		if(a<b){
System.out.println("hello");
		}else{ 
			System.out.print("world");
}
	}
}



예제2

a가 b보다 클 경우 hellow를 출력해라 아니면 world를 출력해라

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		int a=10;
		int b=20;
		
		if(a>b){
			System.out.println("hello");
		}else{
		System.out.print("world");
		}
	}
}

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		int a=10;
		int b=20;
		
		if(a*a!=b*2+10*6) {
			System.out.print(a);
		}else{
			System.out.print(b);
		}
	}
}


예제3

c와 d의 합이 100보다 클경우 "c와d의 합은 100보다 큽니다"를 출력

import java.util.Scanner;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		Scanner s=new Scanner(System.in);
		System.out.print("c:");
		int c=s.nextInt();
		System.out.print("d:");
		int d=s.nextInt();
			
		if(c+d>100){
			System.out.print("c와d의 합은 100보다 큽니다.");
		}
	}
}


예제4

변수 a,b를 만들고 각각 화면에서 입력받는다
a와 b를 합한 값이 짝수이면 a와b를 곱한값을 출력

import java.util.Scanner;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		Scanner s=new Scanner(System.in);
		
		System.out.print("e:");
		int e=s.nextInt();
		System.out.print("f:");
		int f=s.nextInt();
			
		if((e+f)%2==0){
			int g=e*f;
			System.out.print(g);
		}
	}
}


int 변수 isMale을 만들고 화면에 입력받게 한다.

isMale의 값이 1이면 *남자입니다* 출력

import java.util.Scanner;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		Scanner s=new Scanner(System.in);
		
		int isMale;
		int i=s.nextInt();
		
		if(i==1){
			System.out.print("남자");
		}
	}
}


|| or 연산자  - 둘중하나만 맞아도 전체가 맞았다.

&& and 연산자 - 둘다 맞아야 전체가 맞다



예제5

 에버드 나이가 8살 키가 160이상일 경우 "입장가능"이라고 출력

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		int age, height;
		age=8;
		height=160;
		
		if(age>=8&&height>=150){
			System.out.println("입장가능");
		}
	}
}


예제6

 * 가방을 하나 사려고 한다
 * 가방의 가격이 50
 * 현금 상품권
 * 변수를 현금과 상품권 2개 만들고
 * 화면에서 현금과 상품권의 값을 입력받고
 * 둘중에 하나를 지불해서 가방을 살 수 있으면 Ok

import java.util.Scanner;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		Scanner s=new Scanner(System.in);
		
		System.out.print("현금");
		int cash=s.nextInt();
		System.out.print("상품권");
		int gift=s.nextInt();
		
		if(cash>=50||gift>=50){
			System.out.println("Ok");
		}else{
			System.out.println("No");
		}
		
		if(cash+gift>=50){
			System.out.println("Ok");
		}else{
			System.out.println("No");
		}
	}
}

  

 * 현금과 상품권의 합이 50이상 지불해서 가방을 살 수 있으면 Ok

import java.util.Scanner;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		Scanner s=new Scanner(System.in);
		int cash=s.nextInt();
		int gift=s.nextInt();
		
		if(cash+gift>=50){
			System.out.println("Ok");
			
		}else{
			System.out.println("No");
		}
	}
}



예제7

 이병헌, 손연재, 이광수,최홍만 

 *운동선수가 아니고 키가 190미만일경우 이병헌 출력

 *운동선수이고 키가 190미만일 경우 손연재 출력

 *운동선수가 아니고 키가 190이상일 경우 이광수 출력

 *운동선수이고 키가 190이상일 경우 최홍만 출력

import java.util.Scanner;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		Scanner s=new Scanner(System.in);
		
		int isaAthlete=s.nextInt(); //0;  운동선수x 1:운동선수O;
		int height=s.nextInt();
		
		if(isaAthlete!=1&&height<190){
			System.out.print("이병헌");
		}
		else if(isaAthlete==1&&height<=190){
			System.out.print("손연재");
		}
		else if(isaAthlete==1&&height<=190){
			System.out.print("이광수");
		}else if(isaAthlete==1&&height>=190&&height>=190){
			System.out.print("최홍만");
		}else if(isaAthlete==1&&height<=190&&isaAthlete==0&&height<=190){
			System.out.print("이병헌, 손연재");
		}
	}
}



예제8

입력값이 홀수인지 짝수인지 구하기

import java.util.Scanner;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		Scanner s=new Scanner(System.in);
		int a=s.nextInt();
		
		if(a%2==0){
			System.out.print("짝수입니다.");
		}else{
			System.out.print("홀수입니다.");
		}
	}
}


예제9

 *int qustn isMale 만드세요
 * isMale 값이 1이면 "남자" 출력 그렇지 않으면 "여자"출력
  
  

import java.util.Scanner;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		Scanner s=new Scanner(System.in);
		int isMale=s.nextInt();
		
		if(isMale==1){
			System.out.println("남자");
		}else{
			System.out.println("여자");
		}
	}
}

 

  
  예제10

   *중국집에서 탕수육을 시켜먹었습니다.
   * 탕수육의 가격은 12000원입니다.
   * 자신의 현금값을 입력받는 변수를 만들고
   * 현금값이 12000보다 작으면 "무전취식" 출력
   * 현금값이 그 외에는 탕수육 값을 지불하고
   * 남은 금액을 출력
  

import java.util.Scanner;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		Scanner s=new Scanner(System.in);
		int menu=s.nextInt();
		
		if(menu<12000){
			System.out.println("무전취식");
		}else{
			int n=menu-12000;
		
			System.out.println(n);
		}
	}
}


21 mysql 설치 2014.07.23
20 jsp강좌 2014.07.23
19 140719_01 2014.07.17
18 140711_02_자바 2014.07.11
17 140711-자바스크립트 2014.07.11
16 140710-자바스크립트 2014.07.10
15 140709_01 2014.07.09
14 140707 2014.07.07
13 140707 2014.07.07
12 140704_01 2014.07.04
11 140701_01 2014.07.01
10 140626_01 2014.06.26
9 140625_01 2014.06.26
8 140624_01 2014.06.24
7 140623_[java] switch문 2014.06.23
6 140620_[java]랜덤함수 2014.06.20
5 140619_[java]조건문 2014.06.19
> 140618_[java]논리연산자 2014.06.18
3 140617_[java]연산자 2014.06.17
2 140616_[java]상수와 변수 2014.06.16
위로