闰年
1、普通年能被4整除且不能被100整除的为闰年。(如2004年就是闰年,1900年不是闰年)
2、世纪年能被400整除的是闰年。(如2000年是闰年,1900年不是闰年)
public static void main(String[] args) {
// TODO Auto-generated method stub
//String s = new Date().toString();
//System.out.println(s);
/*
int startYear= 1950;
int endYear = 2050;
for ( ; startYear <= endYear; startYear++ )
{
if ((startYear %100 != 0) && (startYear %4 == 0))
{
System.out.printf("%d",startYear);
System.out.println();
}
else if (startYear %400 ==0)
{
System.out.printf("%d",startYear);
System.out.println();
}
}
*/
Scanner input = new Scanner(System.in);
System.out.print("请输入年份:");
int year = input.nextInt();
if((year % 4 == 0 && year % 100 != 0) || year % 400 ==0)
System.out.print(year + "年是闰年。");
else
System.out.print(year + "年不是闰年。");
}
写一个Java程序,计算从1加2,加3…… 一直加到999的总和
public static void main(String[] args) {
// TODO Auto-generated method stub
//String s = new Date().toString();
//System.out.println(s);
int sum = 0;
for (int i = 1; i<=999 ; i++)
{
sum = i + sum;
}
System.out.println("1+2+...+999 = "+ sum);
}