본문 바로가기

JAVA/JAVA

자바 예제 - while 과 for문 : 사용자로부터 생일을 입력받아 별자리 구하는 방법

반응형

사용자로부터 생일을 입력받아 별자리 구하는 방법


<설명>

코드1 : switch 사용 안함

코드2 : switch 사용 함 (코드1 보다 가독성이 좋은 것)

(사람에 따라 느끼는 것이 다를 수도 있음)


<코드1>


import java.util.Scanner;


class Cons{

public static void main(String[] args) {


int month, day;

String name = "";

String cons = "";

String again = "";


Scanner sc = new Scanner(System.in);


while (true){


System.out.print("이름을 입력해");

name = sc.next();


month = 1;

for( ; ; ){

System.out.print("생월을 입력해");

month = sc.nextInt();  


if (month >=1 && month <=12){

break;  

} //end if

month++; 

} // end for


day = 1;

for( ; ; ){

System.out.print("생일을 입력해");

day = sc.nextInt();  


if (day >=1 && day <=31){

break;  

} //end if

day++; 

} // end for


if (month == 1){

if (day >=20){

cons = "물병자리";

}

else cons = "염소자리";

}


if (month == 2){

if (day >=19){

cons = "물고기자리";

}

else cons = "물병자리";

}


if (month == 3){

if (day >=21){

cons = "양자리";

}

else cons = "물고기자리";

}


if (month == 4){

if (day >=20){

cons = "황소자리";

}

else cons = "양자리";

}


if (month == 5){

if (day >=21){

cons = "쌍둥이자리";

}

else cons = "황소자리";

}


if (month == 6){

if (day >=22){

cons = "게자리";

}

else cons = "쌍둥이자리";

}


if (month == 7){

if (day >=23){

cons = "사자자리";

}

else cons = "게자리";

}


if (month == 8){

if (day >=23){

cons = "처녀자리";

}

else cons = "사자자리";

}


if (month == 9){

if (day >=24){

cons = "천칭자리";

}

else cons = "처녀자리";

}


if (month == 10){

if (day >=23){

cons = "전갈자리";

}

else cons = "천칭자리";

}


if (month == 11){

if (day >=23){

cons = "사수자리";

}

else cons = "전갈자리";

}


if (month == 12){

if (day >=25){

cons = "염소자리";

}

else cons = "사수자리";

}


System.out.println(name + " 의 별자리는 " + cons + " 입니다");



while (true){

System.out.println("또 하시겠습니까? [Y / N]");

again = sc.next();


if (again.equals("y") || again.equals("n")){

break;

}


} // end while


if (again.equals("n"))

break;


} //end while


} //end psv


} // end class







/////////////////////////////////


<코드2>


import java.util.Scanner;

class StartTest{

public static void main(String[] args) {

Scanner sc= new Scanner(System.in);

int month, day;

String name, r="", again="";


while(true){

System.out.print("니, 이름머고?");

name = sc.next();


while(true){

System.out.print("몇월달에 태어 났노?");

month = sc.nextInt();


if(month >= 1  &&  month <= 12)

break;

}



//입력한 월에 따른 마지막 날짜를 구한다.

int last=31;

switch(month){

case 2:last = 28;break;

case 4:case 6:case 9:case 11:last = 30;break;

}



while(true){

System.out.print("몇일에 태어 났노?");

day = sc.nextInt();


if(day >= 1  &&  day <= last)

break;

}



switch(month){ 

case 1: if(day  <= 19) r = "염소자리"; else r= "물병자리";  break;

case 2: if(day  <= 18) r = "물병자리"; else r= "물고기자리";  break;


case 3: if(day  <= 20) r = "물고기자리"; else r= "양자리";  break;

case 4: if(day  <= 19) r = "양자리"; else r= "황소자리";  break;


case 5: if(day  <= 20) r = "황소자리"; else r= "쌍둥이자리";  break;

case 6: if(day  <= 21) r = "쌍둥이자리"; else r= "게자리";  break;


case 7: if(day  <= 22) r = "게자리"; else r= "사자자리";  break;

case 8: if(day  <= 22) r = "사자자리"; else r= "처녀자리";  break;


case 9: if(day  <= 23) r = "처녀자리"; else r= "천칭자리";  break;

case 10:if(day  <= 22) r = "천칭자리"; else r= "전갈자리";  break;


case 11:if(day  <= 22) r = "전갈자리"; else r= "사수자리";  break;

case 12:if(day  <= 24) r = "사수자리"; else r= "염소자리";  break;

}


System.out.println(name + "님의 별자리는 " + r + "입니다.");


while(true){

System.out.print("또 하시겠습니까? (y/n)");

again = sc.next();


if(again.equals("y") || again.equals("n"))

break;

}


if(again.equals("n"))

break;

}


System.out.println("프로그램을 종료합니다.");

}

}



반응형