본문 바로가기

자바/문법

if 연속문(세가지 숫자를 입력 받아 작은수부터 출력)

예제)


import java.io.IOException;

import java.io.InputStreamReader;

import java.io.BufferedReader;


public class Test13 {

public static void main(String[] args) throws IOException {

BufferedReader br=new BufferedReader(

new InputStreamReader(System.in));


int a,b,c;

int temp;


System.out.print("첫번째 수 ?");

a=Integer. parseInt(br.readLine());


System.out.print("두번째 수 ?");

b=Integer. parseInt(br.readLine());


System.out.print("세번째 수 ?");

c=Integer. parseInt(br.readLine());


/*  원시적인 방법

if(a<b&&b<c) {

System.out.println("결과:"+a+" "+b+" "+c);

} else if(a<c&&c<b) {

System.out.println("결과:"+a+" "+c+" "+b);

} else if(b<a&&a<c) {

System.out.println("결과:"+b+" "+a+" "+c);

} else if(b<c&&c<a) {

System.out.println("결과:"+b+" "+c+" "+a);

} else if(c<a&&a<b) {

System.out.println("결과:"+c+" "+a+" "+b);

} else if(c<b&&b<a) {

System.out.println("결과:"+c+" "+b+" "+a);

} else

System.out.println("세 숫자 중 2개이상이 같음");

*/


if(b>a) {

temp=a; a=b; b=temp; // temp라는 빈곳에 a를 담아서 작은 순서대로 바꿔주었다.

}

if(a>c) {

temp=a; a=c; c=temp;

}

if(b>c) {

temp=b; b=c; c=temp;

}

System.out.printf("%d %d %d\n",a,b,c);


System.out.println("종료 !!!");


}

}


결과) 세가지 수를 입력받아 작은 순서대로 출력


첫번째 수 ?10

두번째 수 ?5

세번째 수 ?4

4 5 10

종료 !!!

'자바 > 문법' 카테고리의 다른 글

while 문법  (0) 2013.05.09
if ~ else 문(평년, 윤년)  (0) 2013.05.09
if ~ else 문(숫자를 입력받아 절대값으로 출력)  (0) 2013.05.09
if ~ else if문(큰수 작은수 판별)  (0) 2013.05.09
if ~else if 문(수우미양가)  (0) 2013.05.09