2 Replies - 1321 Views - Last Post: 23 October 2009 - 08:50 PM Rate Topic: -----

#1 quyetnguyen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-October 09

Method to determine number of days in a month

Posted 23 October 2009 - 05:08 AM

This is exercise ,Who can help me test ? thanks you.

4. Problem 04: Day of Month
- Class-name: cDayOfMonth_<mssv>
- Requirment: print out the number days of a month.
- Input: Month (an integer) and Year (integer) from Console
- Output:
o If this month is not valid (không hợp lệ) then print out:
<month>/<year> is invalid year.
o Else: print out the number days of this month
<month>/<year> has <number-day> days.
- Note: becarful with leap year.
- Example:
o Ex1:
Input:
• Month: 13
• Year: 2009
Output: 13/2009 is invalid month.
o Ex2:
Input:
• Month: 8
• Year: 2010
Output: 8/2010 has 31 days.
o Ex3:
Input:
• Month: 2
• Year: 1200
Output: 2/1200 has 29 days.


This is code of myself.
import java.util.*; 
public class test4 {	  /** 	 * @param args 	 */ 	
public static void main(String[] args) { 		
// TODO Auto-generated method stub 	
Scanner in=new Scanner(System.in); 		
int month, year; 		
System.out.print("Input moth month : "); 		
month=in.nextInt(); 		
System.out.print("Input year year: "); 		
year=in.nextInt(); 		
if(month<0&&month<=13){ 			
System.out.print(month+"/"+year+" is invalid year "); 		
} 		
else{ 			if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){ 				System.out.print(month+"/"+year+" has 31"); 			
} 			
if(month==4||month==6||month==9||month==11){ 				
System.out.print(month+"/"+year+" has 30 "); 				 			
} 			
if(month==2&&(year%4==0&&year%100!=0||year%400==0))
{ 				
System.out.print(month+"/"+year+" has is 28"); 			
} 			 		
} 	
}  
}


*Edited: to put a more describtive Topic Title

This post has been edited by pbl: 23 October 2009 - 08:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Method to determine number of days in a month

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,294
  • Joined: 27-December 08

Re: Method to determine number of days in a month

Posted 23 October 2009 - 06:31 AM

Please use proper indentation when posting your code. It is extremely hard to read. For your days, I'd use a switch statement like so:

//Returns num days in month or -1 if invalid month
public int daysInMonth(int month, int year){
  switch(month){
	 case 1:
	 case 3:
	 case 5:
	 case 7:
	 case 8:
	 case 10:
	 case 12:
		 return 31;
	 default: return -1;
   ..cases for months w/30 days and february..
  }
}



This is easier to read than an if statement with 5 or 6 conditions.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Method to determine number of days in a month

Posted 23 October 2009 - 08:50 PM

And please respect rule #5 of this forum

Give your topic a descriptive title, NOT "Help" or "Plz Help Me" or "I'm a noob"

"Help me" is kind of useless... and it is not the first time I told you :angry:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1