//【程序14】 TestDay.java 题目:输入某年某月某日,判断这一天是这一年的第几天?
//1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
//闰年共有366天(1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天)
import javax.swing.JOptionPane;
public class TestDayApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestDay td=new TestDay();
td.countDay();
}
}
class TestDay{
int year,month,day;int total;
int[] array={31,28,31,30,31,30,31,31,30,31,30,31};
TestDay(){
year=Integer.parseInt(JOptionPane.showInputDialog("请输入年份(样式为“2014”):"));
month=Integer.parseInt(JOptionPane.showInputDialog("请输入月份(样式为“9”):"));
day=Integer.parseInt(JOptionPane.showInputDialog("请输入某日(样式为“23”):"));
}
public boolean isLeapYear(int year){
return (year%4==0&&year%100!=0)||(year%400==0);
}
public void countDay(){
if (isLeapYear(year))
array[1]=29;
for(int i=0;i<month-1;i++)
total+=array[i];
total+=day;
JOptionPane.showMessageDialog(null, "总的天数为:\n"+total);
}
}
【程序14】 TestDay.java 题目:输入某年某月某日,判断这一天是这一年的第几天?
最新推荐文章于 2022-04-02 15:32:46 发布