Web Analytics Made Easy -
StatCounter help with my if statements - CodingForum

Announcement

Collapse
No announcement yet.

help with my if statements

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • help with my if statements

    so my program is checking the month, day and year the user is inputting if it is correct, however, currently, its not working

    i believe the problem is the if, else if statements
    what im trying to do is if the month (mm is 4,6,9,11, then the day cannot be greater than 31) or if (mm is 1,3,5,7,8,10, then day (dd) cannot be greater than 30)

    however, if it enter 4 for month, and 14 for day, and 2009 for year, i get this outputted
    Error: Day cannot be greater than 30 if Month is either 4, 6, 9, or 11

    which is clearly wrong

    Code:
    import java.util.Scanner;
    public class test
    {
    	public static void main	(String[] args)
    	{
    	int mm, dd,	yyyy;
    	boolean leapyear = false;
    	Scanner keyboard = new Scanner(System.in);
    		System.out.println("Enter the	Month");
    			mm	= keyboard.nextInt();
    		System.out.println("Enter the	Day");
    			dd	= keyboard.nextInt();
    		System.out.println("Enter the	Year");
    			yyyy = keyboard.nextInt();
    		int checkleap;
    		checkleap =	yyyy % 4;
    		System.out.println(checkleap);
    			if	(checkleap == 0)
    				leapyear	= true;
    			else if (mm	==	4 || mm == 6 || mm == 9	||	mm	==	11	&&	dd	> 30)
    				System.out.println("Error:	Day cannot be greater than	30	if	Month	is	either 4, 6, 9, or 11");
    			else if (mm	==	1 || mm == 3 || mm == 5	||	mm	==	7 || mm == 8 || mm == 10 || mm == 12 && dd >	31)
    				System.out.println("Error:	Day cannot be greater than	31	if	Month	is	either 1, 3, 5, 7, 8, 10, or 12");
    			else if (mm	==	2 && leapyear == true && dd >	29)
    				System.out.println("Error:	Day cannot be greater than	29	on	a leap year");
    			else if (mm	==	2 && leapyear == false && dd > 28)
    				System.out.println("Error:	Day cannot be greater than	28	in	February	on	a nonleap year");;	
    	}
    }//end program
    Last edited by raptrex; Apr 15, 2009, 02:05 AM.

  • #2
    you need to paranthesize ur or expressions otherwise if even one condition is satisfied the rest of the condition will not be evaluated
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

    Comment

    Working...
    X