자바 계절 ...ㅠㅠ
헛매질
The seasons in the Northern Hemisphere typically occur during the following date ranges:
Winter:December 22 to March 19
Spring:March 20 to June 20
Summer:June 21 to September 20
Autumn:September 21 to December 21
Write a program that asks the user for the month and day as integers and then displays the season for that date.
Sample output follows:
This program reports the season for a given day and month.
Type the month and day as integers separated by spaces: 7 22
Month 7, Day 22 is in the summer.여기서 뭘 고쳐야......위같은 결과가 나올까요??
어떤 부분을 고쳐야 할지 ...ㅠㅠ 조언 부탁드리겠습니다.
제가 독학중이라..많이 부족합니다.... 죄송합니다.
import java.io. * ;
public class IF {
public static void main (String args[]){
String Mtos =; //계절
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int month= 0;//사용자로부터 입력받은 수를 저장 할 수 있는 변수 month를 선언한다.
while (month==0) //초기값 0
{
System.out.println(This program reports the season for a given day and month. ); // 구하고 싶은 달 입력
System.out.print(Type the month and day as integers separated by spaces : ); // 구하고 싶은 달 입력
String num = in.readLine(); //사용자의 키보드로부터 입력 받는다.
month = Integer.parseInt(num); //문자열로 받은 num을 정수로 변환하여 x에 저장한다.
if (month =13 || month 1)//12보다 큰 수 또는 1보다 작은수가 입력되면
{
System.out.println(다시 입력해주세요);
month =0; //초기값 0이므로 0으로...
}
if (month ==12 || month ==1 || month ==2)
Mtos = 겨울;
else if (month == 3 || month == 4 || month ==5)
Mtos = 봄;
else if (month == 6 || month ==7 || month ==8)
Mtos= 여름;
else if (month == 9 || month == 10 || month ==11)
Mtos =가을;
}
System.out.print(month+월은+Mtos +이다);
}
catch (IOException ie) //예외 처리
{
System.out.println(에러 입니다.);
}
}
}
-
늘봄
사용자에게 입력받는 변수인 num을 한번 출력해보세요... 입력한 형식대로 출력이 될것입니다. 7 22 입력을 하면 7 22 이 값이 그대로 찍히기 때문에 month로 형변환을 할시 에러가 발생합니다. 에러를 방지하시려면 당연히 입력되는 값을 일단 쪼개야 겠죠.. 그것에 대한 정보는 spilt 또는 StringTokenizer을 찾아보시기 바랍니다.