메뉴 건너뛰기

HEUKMYO

program1

140619_[java]조건문

2014.06.19 15:11 read.59

※ java 책 추천

JSP 웹프로그래밍 입문 활용 (JSPStudy의)


조건문

if(조건문){

   }


조건문에 내용이 맞으면 {} 안에 내용 진행

if(조건문){

}

{}구간안에 있는 변수는 {}안에서만 적용


if(조건문){

a

}

else{

b

}

조건문에 만족할때 a ,조건문에 만족하지 않을때 b


예제1
a가 b보다 작은 경우 flag는 true, a가 b보다 클경우는 flag는 false 출력
public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		int a=1,b=2;
		boolean flag;
		flag = false;

		if (a < b){
			flag = true;
		}
		System.out.println("Flag is : " + flag);
	}
}


출력값 :Flag is :  true

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		int a=1,b=-1;
		boolean flag;
		flag = false;

		if (a < b){
			flag = true;
		}
		System.out.println("Flag is" + flag);
	}
}


출력값 :Flag is :  false

예제2
isMale값을 입력받아 0이면 여자회원, 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=0;
		System.out.println("회원가입");
		System.out.println("성별 0:여자, 1:남자");
		isMale=s.nextInt();
		
		if(isMale==1){
			System.out.print("남자회원");
		}else{
			System.out.print("여자회원");
		}
	}
}



예제3-1

현금인출기(비밀번호가 맞으면 OK, 틀리면 NO 출력)

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 comPw=1234;  //컴퓨터가 알고 있는 내 비밀번호
		int myPw;
		System.out.println("안녕하세요. ATM입니다.");
		System.out.println("비밀번호를 입력 : ");
		
		myPw=s.nextInt();
		
		if(comPw==myPw){
			 System.out.println("OK");
		}else{
			System.out.println("NO");
		}
	}
}

예제3-2

현금인출기(비밀번호가 맞으면 예금, 출금 선택하고 비밀번호가 틀리면 "비밀번호가 틀렸습니다. ATM 종료" 출력.)

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 comPw=1234;  //컴퓨터가 알고 있는 내 비밀번호
		int myPw;
		System.out.println("안녕하세요. ATM입니다.");
		System.out.println("비밀번호를 입력 : ");
		
		myPw=s.nextInt();
		
		if(comPw==myPw){
			System.out.println("예금 : 1, 출금 : 2 를 누르세요" );
			System.out.print("입력");
		
			int InOut=s.nextInt();
				
			if(InOut==1){
				System.out.println("예금");
			}else if(InOut==2){
				System.out.println("출금");
			}else{
				System.out.println("잘못입력했습니다. ATM 종료");
			}
		}else{
			System.out.println("비밀번호가 틀렸습니다. ATM 종료");
		}
	}
}

예제3-3

현금인출기(비밀번호가 맞으면 예금, 출금 선택하고 예금일 경우 입금금액입력, 출금일경우 출금금액 입력 후 잔액 출력한다. 비밀번호가 틀리면 "비밀번호가 틀렸습니다. ATM 종료" 출력.)

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 comPw=1234;  //컴퓨터가 알고 있는 내 비밀번호
		int myPw;
		int balance=50000;
		
		System.out.println("안녕하세요. ATM입니다.");
		System.out.println("비밀번호를 입력 : ");
		
		myPw=s.nextInt();
		
		if(comPw==myPw){
			System.out.println("예금 : 1, 출금 : 2 를 누르세요" );
			System.out.print("입력");			 
		 
			int InOut=s.nextInt();
			
			if(InOut==1){ //예금일경우
				System.out.println("현금얼마를 입력할까요?");
				System.out.print("입력");
				int cash=s.nextInt();
				balance=balance+cash;
				System.out.println("잔액"+balance+"원");
			}else if(InOut==2){
				System.out.println("현금얼마를 찾으시겠습니까?");
				System.out.print("입력");
				int cash=s.nextInt();
				if(balance>=cash){ //잔액이 있는 경우
					System.out.println("찾은금액"+cash);
					balance=balance-cash;
					System.out.print("잔액"+balance+"원");
				}
			}else{
				System.out.println("잘못입력했습니다. ATM 종료");
			}
		}else{
			System.out.println("비밀번호가 틀렸습니다. ATM 종료");
		}
	}
}


예제4

A라는 곳에서 B라는 곳으로 갑니다.

B라는 곳에서 C라는 곳으로 갑니다.

각각 이동할때마다 택시, 버스를 탈지 선택할 수 있습니다.

택시,버스, 그외를 입력 받아서 교통비를 계산

A -> B 택시를 타면 10000원 버스를 타면 1150원 그외 300원

B -> C 택시를 타면 15000원 버스를 타면 1150원 그외 400원

import java.util.Scanner;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner s=new Scanner(System.in);
		
		System.out.println("A에서 B로 이동합니다. 어떤 교통을 이용하시겠습니까?");
		System.out.println("1. 택시, 2. 버스, 3. 그외는 3");
		int m=s.nextInt();
		System.out.println("B에서 C로 이동합니다. 어떤 교통을 이용하시겠습니까?");
		System.out.println("1. 택시, 2. 버스, 3. 그외는 3");
		int mo=s.nextInt();
		
		 if(m==1){
			int mtaxi=10000;
			if(mo==1){
				int motaxi=15000;
				System.out.print("총교통비"+(mtaxi+motaxi)+"원");
			}
			else if(mo==2){
				int mobus=1150;
				System.out.print("총교통비"+(mtaxi+mobus)+"원");
			}
			else{
				int e=300;
				System.out.print("총교통비"+(mtaxi+e)+"원");
			}
		}
		else if(m==2){
			int mbus=1150;
			if(mo==1){
				int motaxi=15000;
				System.out.print("총교통비"+(mbus+motaxi)+"원");
			}
			else if(mo==2){
				int mobus=1150;
				System.out.print("총교통비"+(mbus+mobus)+"원");
			}
			else{
				int e=300;
				System.out.print("총교통비"+(mbus+e)+"원");
			}
		}
		else {
			int me=300;
			if(mo==1){
				int motaxi=15000;
				System.out.print("총교통비"+(me+motaxi)+"원");
			}
			else if(mo==2){
				int mobus=1150;
				System.out.print("총교통비"+(me+mobus)+"원");
			}
			else{
				int e=300;
				System.out.print("총교통비"+(me+e)+"원");
			}
		}
	}
}


예제5

회원가입되어있다고 생각하고 컴퓨터는 가입자의 아이디를 gagaoffice라고 알고 있다.

컴퓨터는 가입자의 비밀번호를 1212라고 알고 있다

로그인 하는 프로그램을 만드세요

아이디가 틀릴경우 -> "그런 회원 없다"출력

비밀번호가 틀릴경우 ->"비밀번호가 틀립니다" 출력

둘다 맞을 경우 -> 로그인 되었습니다 출력

스트링 값 비교

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);

		String comId="gagaoffice";
		int comPw=1212;
		
		System.out.println("아이디를 입력하세요");
		String myId=s.next();
		
		if(comId.equals("gagaoffice")){

			System.out.println("비밀번호를 입력하세요");
			int myPw=s.nextInt();
			if(comPw==myPw){
				System.out.print("로그인 되었습니다");
			}else{
				System.out.print("비밀번호가 틀립니다.");
			}
		}else{
			System.out.print("그런회원 없습니다");
		}
	}
}

comId.equals("gagaoffice") comId가 ""안의 값과 같은 문자인지 비교해준다.

예제6
두수를 입력받아서 첫번째 수 나누기 두번째 수 소수 2번째 자리까지 구하세요(a모든 자료형은 int)
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.println("첫번째 숫자를 입력 : ");
		int fi =s.nextInt();
		System.out.println("두번째 숫자를 입력 : ");
		int se =s.nextInt();
		
		int re= (fi*100)/se;
		System.out.print((re/100)+"."+(re%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.println("첫번째 숫자를 입력 : ");
		int fi =s.nextInt();
		System.out.println("두번째 숫자를 입력 : ");
		int se =s.nextInt();
		
		int re= (fi*100)/se;
		System.out.print((re/100)+"."+(re%100));
	}
}


예제7
입사서류전형
1. 학점검사
학점 평점이 3.5미만이면 -> 입사지원 불가능
학점 평점이 3.5이상이면 다음단계
2. 토익검사
토익점수가 700 미만이면 ->입사지원불가능
토익점수가 700 이상 -> 다음단계
3. 체력검사
100m 달리기 20초 이하면 합격
100m 달리기가 20초 초과면 불합격
총 입시점수는 학점 *1000+토익점수+100m기록
import java.util.Scanner;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		Scanner b=new Scanner(System.in);
		
		System.out.print("한점 평점을 입력하세요");
		float grade=b.nextFloat();
		
		if(grade<3.5){
			System.out.print("입사지원이 불가능합니다.");
			
		}else{
			System.out.print("토익점수를 입력하세요");
			int toeic=b.nextInt();
			
			if(toeic<700){
				System.out.print("입사지원이 불가능합니다.");
			}else{
				System.out.print("100m기록을 입력하세요");
				int sec=b.nextInt();
				if(sec>20){
					System.out.print("불합격");
				}else{
					System.out.print("합격");
					System.out.print("총 입시점수:"+ (grade*1000+toeic-sec));
				}
			}
		}
	}
}


  1. [2014/06/23] 140623_[java] switch문 by ()
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
> 140619_[java]조건문 2014.06.19
4 140618_[java]논리연산자 2014.06.18
3 140617_[java]연산자 2014.06.17
2 140616_[java]상수와 변수 2014.06.16
위로