java学习编程记录一

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);
    }
}

}
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值