Results 1 to 5 of 5
Thread: 'if' statement not working?
- 03-10-2012, 02:47 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
'if' statement not working?
The 'if' statement part of the code isnt working.
I want it to change the day to 1 and the month to the next month when the day equals 32 or 31 depending on how many days are in the month(32 and 31 referring to the day after the month ends).
DD = day of the month
MM = month
dont worry about DAY and PUs.
Also i wont need to go into the next year so dont worry about helping with that.
THANKS!Java Code:package packageA; public class d9 { public static void main(String[]args){ int DD = 9; int MM = 3; int DAY = 1; int PUs = 29; int count = 1; System.out.println("MM/DD/YYYY, ###, #PU's\n----------------------"); while(count < 40){ System.out.printf("%02d/%02d/2012, %03d, %05d\n", MM, DD, DAY, PUs); DD++; DAY++; PUs++; if(MM == (1|3|5|7|8|10|12)){ if(DD == 32){ DD = 1; MM += 1; } }else{ if(MM == (4|6|9|11)){ if(DD == 31){ DD = 1; MM += 1; } } } count++; } } }Last edited by Etimer; 03-10-2012 at 02:54 AM.
-
Re: 'if' statement not working?
Clean up your code a bit and compartmentalise it so that it becomes easier to understand.
I would use a switch statement instead of what you have done for the number of days in the month, and throw that into a separate method so that you can more clearly identify the problem.
Once you've done that you can use the method like this:Java Code:private static final int MONTH_ERROR = -10; public int getLastDay(int month, int year) { switch (month) { case 1: //jan case 3: //mar case 5: //may case 7: //july case 8: //aug case 10: //oct case 12: //dec return 31; case 4: //apr case 6: //jun case 9: //sep case 11: //nov return 30; case 2: //feb return (isLeapYear(year)? 29 : 28); default: return MONTH_ERROR; } } public boolean isLeapYear(int year) { ... }
And from that decide what to do:Java Code:public boolean isLastDayOfMonth(int day, int month, int year) { int lastDay = getLastDay(month, year); if (day == lastDay) { return true; } return false; }
Java Code:int day = 10; int month = 3; int year = 2012; if (isLastDayOfMonth(day,month,year)) { //set to first day of next month day = 1; month++; }
- 03-10-2012, 03:45 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Re: 'if' statement not working?
THANK YOU VERY MUCH SIR!
Although, i don't understand why what i used wouldnt work?
-
Re: 'if' statement not working?
Read this:
Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
and find the difference between the operator || and |.
- 03-10-2012, 05:18 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01-31-2012, 08:36 PM -
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks