1.提示用户输入年月日信息,判断这一天是这一年中的第几天并打印
/*
编程实现日期判断
*/
import java.util.Scanner;
public class jtest{
public static void main(String []args){
//声明变量
int year;
int month;
int day;
int[] mon = new int[12];
int sum = 0;
int tw;
//输入日期
System.out.println("请输入日期:");
Scanner sc=new Scanner(System.in);
year =sc.nextInt();
month =sc.nextInt();
day = sc.nextInt();
//根据是否是闰年确定二月份天数
if((year % 4 == 0)&&(year % 100 != 0)||(year % 400 == 0))
tw = 29;
else
tw = 28;
for(int i=0;i<12;i++){
switch(i){
case 0:
case 2:
case 4:
case 6:
case 7:
case 9:
case 11:
mon[i] = 31;
break;
case 3:
case 5:
case 8:
case 10:
mon[i] = 30;
break;
case 1:
mon[i] = tw;
break;
}
}
for(int i=0;i < (month-1);i++){
sum = sum + mon[i];
}
sum += day;
System.out.print("输入日期为一年中的第");
System.out.print(sum);
System.out.println("天");
}
}
2.编程找出 1000 以内的所有完数并打印出来,完数就是一个数恰好等于它的因子之和,如:6=1+2+3
/*
编程实现寻找1000以内的完数
*/
public class twotest{
public static void main(String[] args){
for(int i = 1;i<=1000;i++){
int sum = 0;
for(int j = 1;j<i;j++){
if(i%j == 0)
sum = sum + j;
else
sum = sum;
}
if(i == sum)
System.out.println(i);
}
}
}