[RESOLVED] Switch statement to check month
Hi,
I made this code for a home work exercise that uses a switch statement to choose the correct number of days in a month. This is so I can use it to return the number of days between two dates. However, my code is not working and I don't know why.
Code:
public class Year {
protected int daysinyear;
protected int daysLapsed;
protected int month;
protected int days;
public Year (){
daysinyear = 365;
month = 3;
}
public date checkmonth (month){
switch (month) {
case 1:
return days =31;
case 2:
return days =28;
case 3:
return days =31;
case 4:
return days =30;
case 5:
return days =31;
case 6:
return days =30;
case 7:
return days =31;
case 8:
return days =31;
case 9:
return days =30;
case 10:
return days =31;
case 11:
return days =30;
case 12:
return days =31;
}
}
Thanks,
Nightwalker
Re: Switch statement to check month
I think the problem is with the return statements:
I think you could rewrite it like this:
Code:
public int checkmonth (int myMonth){
switch (myMonth) {
case 1:
days =31;
break;
case 2:
days =30;
break;
case 3:
days =31;
break;
//....etc
return (days);
}
}
untested code :wave:
Re: Switch statement to check month
Yes, you need a break; between each of your cases.
Might want to look at the example here... may help to simplify and shorten your code some: http://download.oracle.com/javase/tu...ts/switch.html
-tg
Re: Switch statement to check month
I was told by my lecturer to leave the breaks out otherwise the days in each month in between the original date and the target date wouldn't be added together. I may have misunderstood what he meant though.
Edit:
This is how I did of the months and it works although it might be a bit longwinded.
Code:
public class Year {
protected int daysinyear;
protected int daysLapsed = 0;
public Year (){
daysinyear = 365;
int month = 5;
switch (month) {
case 1:
daysLapsed = daysLapsed + 0;
break;
case 3:
daysLapsed = daysLapsed + 59;
break;
case 5:
daysLapsed = daysLapsed + 120;
break;
case 7:
daysLapsed = daysLapsed + 181;
break;
case 8:
daysLapsed = daysLapsed + 212;
break;
case 10:
daysLapsed = daysLapsed + 273;
break;
case 12:
daysLapsed = daysLapsed + 334;
break;
case 4:
daysLapsed = daysLapsed + 89;
break;
case 6:
daysLapsed = daysLapsed + 150;
break;
case 9:
daysLapsed = daysLapsed + 242;
break;
case 11:
daysLapsed = daysLapsed + 303;
break;
case 2:
daysLapsed = daysLapsed + 28;
break;
}
System.out.println("Number of Days = " + daysLapsed);
}
}
At least I had a go. Now I have to the same for the days.
Re: [RESOLVED] Switch statement to check month
When you add break, when the program hits the break line, it will stop. Leaving these break lines causes the program to basically flow into the next case. If you don't want this behaviour (mostly you don't want to in normal situations), then you would consider adding a break. If you are adding stuff along the cases, then it is probably a good idea to leave out the break.
As for your original problem, you are trying to do an assignment and a return. I'm not too sure, but perhaps it would have worked out if you used return (days = 31), but even then I'm not entirely sure, as I've never used Java that way (more like the way akieshbc has done).
One final thing, if you want to add a value to it's own variable, like you've done, you can save yourself a bit of typing hassle and make code look a bit more friendlier by writing
rather than
Code:
daysLapsed = daysLapsed + 1
At the end of the day, they both do the same thing, it's just the former is syntactic sugar.
Re: [RESOLVED] Switch statement to check month
Thanks! I keep forget thing the += way of doing things.