|
-
Nov 4th, 2010, 02:33 AM
#1
[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
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 4th, 2010, 04:59 AM
#2
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
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Nov 4th, 2010, 05:30 AM
#3
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
-
Nov 4th, 2010, 05:35 PM
#4
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.
Last edited by Nightwalker83; Nov 4th, 2010 at 06:57 PM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 10th, 2010, 05:57 PM
#5
Fanatic Member
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.
-
Nov 11th, 2010, 01:35 AM
#6
Re: [RESOLVED] Switch statement to check month
Thanks! I keep forget thing the += way of doing things.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|