JAVA 조건문 연습문제

어뮤즈온·2020년 11월 30일
0

초급자바

목록 보기
9/31
post-custom-banner

1 ~ 100 사이의 랜덤한 숫자를 3개 발생시키고 오름차순으로 출력하시오.

int ran = (int)(Math.random() * 100) + 1;
int ran2 = (int)(Math.random() * 100) + 1;
int ran3 = (int)(Math.random() * 100) + 1;
		
int fir = 0;
int sec = 0;
int thr = 0;
		
if(ran < ran2){
	if(ran2 < ran3){
		fir = ran;
		sec = ran2;
		thr = ran3;
	}else if(ran3 < ran){
		fir = ran3;
		sec = ran;
		thr = ran2;
	}else{
		fir = ran;
		sec = ran3;
		thr = ran2;
	}
}else if(ran2 < ran){
	if(ran < ran3){
		fir = ran2;
		sec = ran;
		thr = ran3;
	}else if(ran3 < ran2){
		fir = ran3;
		sec = ran2;
		thr = ran;
	}else{
		fir = ran2;
		sec = ran3;
		thr = ran;
	}
}
		
System.out.println(fir + " < " + sec + " < " + thr);


//-----------------샘 풀이-----------------
int x = (int)(Math.random() * 100) + 1;
int y = (int)(Math.random() * 100) + 1;
int z = (int)(Math.random() * 100) + 1;
		
if(x > y){
	int temp = y;
	y = x;
	x = temp;
}
		
if(x > z){
	int temp = x;
	x = z;
	z = temp;
}
		
if(y > z){
	int temp = y;
	y = z;
	z = temp;
}
		
System.out.println(x + "<" + y + "<" + z);
profile
Hello, world!
post-custom-banner

0개의 댓글