刚开始学习JAVA,遇到问题了,麻烦大家帮忙!
程序内容是根据年月计算天数,代码如下。
import java.io.*;
import java.util.*;
public class Date
{
public static void main(String[] args) throws Exception
{
int year,month,day,sum,y,i;
int[][] a =new int[][]{
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
year = Integer.parseInt(br.readLine());
month = Integer.parseInt(br.readLine());
day = Integer.parseInt(br.readLine()); if (year % 4 == 0 &&(year % 100 != 0 || year % 400 == 0))
y=0;
else
y=1; for (sum=0,i = 1 ; i < month; i++)
sum=sum+month[y][i];
sum += day;
System.out.println("sum="+sum); }
}
程序内容是根据年月计算天数,代码如下。
import java.io.*;
import java.util.*;
public class Date
{
public static void main(String[] args) throws Exception
{
int year,month,day,sum,y,i;
int[][] a =new int[][]{
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
year = Integer.parseInt(br.readLine());
month = Integer.parseInt(br.readLine());
day = Integer.parseInt(br.readLine()); if (year % 4 == 0 &&(year % 100 != 0 || year % 400 == 0))
y=0;
else
y=1; for (sum=0,i = 1 ; i < month; i++)
sum=sum+month[y][i];
sum += day;
System.out.println("sum="+sum); }
}
核心代码如下:
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.set(c1.YEAR,y);
c1.set(c1.MONTH,M);
c1.set(c1.DATE,d);c2.set(c1.YEAR,y);
c2.set(c1.MONTH,0);
c2.set(c1.DATE,0);for(int i=0; ;i++)
{
if(c1.compareTo(c2)==0)
{
System.out.println("天数为:"+i);
break;
}
else
{
c2.add(c2.Date,1);
}
}
大致就是这样了.
我刚开始学,还没学到calendar类。
Calendar c = Calendar.getInstance();
c.set(2009,10,13);
System.out.println(c.get(Calendar.DAY_OF_YEAR));