Created
October 29, 2010 16:30
-
-
Save Feuda/653852 to your computer and use it in GitHub Desktop.
重点一个月多少天里的规律
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BreakInSwitch { | |
public int days(int year, int month) { | |
int days = 0; | |
switch (month) { | |
case 1: | |
case 3: | |
case 5: | |
case 7: | |
case 8: | |
case 10: | |
case 12: | |
days = 31; | |
break; | |
case 4: | |
case 6: | |
case 9: | |
case 11: | |
days = 30; | |
break; | |
case 2: | |
if (year % 4 ==0 && year % 100 != 0 || year % 400 == 0) { | |
days = 29; | |
} else { | |
days = 2; | |
} | |
break; | |
} | |
return days; | |
} | |
} | |
import javax.swing.JOptionPane; | |
public class BreakDemo1 { | |
public static void main(String[] args) { | |
BreakInSwitch ob = new BreakInSwitch(); | |
String strYear = JOptionPane.showInputDialog("请输入一个年份:"); | |
String strMonth = JOptionPane.showInputDialog("请输入一个月份:"); | |
int year = Integer.parseInt(strYear); | |
int month = Integer.parseInt(strMonth); | |
int days = ob.days(year, month); | |
JOptionPane.showMessageDialog(null, year + "年" + month + "月有" + days + "天!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
可以用 Java 8 的 YearMonth