接收年月,输出当月日历

1.  从键盘输入三位整数,输出最大数。

import java.util.Scanner;

public class one {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		System.out.print("输入三个数:");
		int num1=sc.nextInt();
		int num2=sc.nextInt();
		int num3=sc.nextInt();
		int max=num1;
		
		if (num2>num1)
			max=num2;
		
		if (num3>num1)
			max=num3;
		
		System.out.print("输出最大数:"+max);

	}
}


2.  从键盘输入任意整数,计算该数各位数之和。

public class two {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		System.out.print("输入一个数:");		
		int num =sc.nextInt();
		int sum=0; //计算和
		while (num!=0){
			sum+=num%10;
			num=num/10;
		}
		System.out.print("输出该数各位数之和:"+sum);	
}
}


3.  设计程序实现将十进制整数转为二进制输出。

public class three {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		System.out.print("输入一个数:");		
		int num =sc.nextInt();
		String binarynum=toBinary(num);//十进制整数转为二进制输出
		System.out.print("输出该数二进制数:"+binarynum);	
	}
	public static String toBinary(int num){
		String str="";
		int re;
		while (num!=0){
			re= num%2;
			str=re+str;
			num=num/2;
		}
		return str;
	}
}


4.  键盘输入任意整数,判断该数是否为素数。
        [素数是指只能被1和它本身整除的数]

public class four {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		System.out.print("输入一个数:");		
		int num =sc.nextInt();
		//判断次数是否为素数
		int n=2;
		while(num%n!=0){
			n++;
		}
		if(n==num)
			System.out.print(num+"是素数");	
		else
			System.out.print(num+"不是素数");		
	}
}


5.  接收年月,输出当月日历,效果如下:
      (  已知1900年1月1日  星期一)

请选择年份:2020
请选择月份:  2

星期日 星期一 星期二 星期三 星期四 星期五 星期六

                  1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29

public class five {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份(>=1900):");
		int year = sc.nextInt();//获取年份
		System.out.println("请输入月份[1-12]:");
		int month = sc.nextInt();//获取月份
		//1、对应月份的天数
		int days = 28;
		if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
			days = 31;
		}else if(month==4||month==6||month==9||month==11){
			days = 30;
		}else{
			if(year%400==0||(year%4==0&&year%100!=0)){//判断闰年的情况
				days = 29;
			}
		}

		//2、相差天数
		int yDays = 0;//统计两个年份之间相差的天数
		for(int y = 1900;y<year;y++){
			if(year%400==0||(year%4==0&&year%100!=0)){//判断闰年的情况
				yDays += 366;
			}else{
				yDays += 365;
			}
		}

		int mDays = 0;//指定年份1月1日,到指定月份1日相差的天数
		for(int m = 1;m<month;m++){
			if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){
				mDays += 31;
			}else if(m==4||m==6||m==9||m==11){
				mDays += 30;
			}else{
				if(year%400==0||(year%4==0&&year%100!=0)){//判断闰年的情况
					mDays += 29;
				}else{
					mDays += 28;
					}
		}
		}

		int totalDays = yDays + mDays;//相差总天数
		int week = totalDays%7+1;//指定月份1号 的星期
		System.out.println("日\t一\t二\t三\t四\t五\t六");
		//先根据星期 确定需要打印空位的 情况(week)
		for(int i = 1;i<=week;i++){
			if(i%7==0){
				System.out.println(" \t");
			}else{
				System.out.print(" \t");
			}
		}
		//可以打印对应月份的天数(days)
		for(int j = 1;j<=days;j++){
			if((j+week)%7==0){
				System.out.println(j+"\t");
			}else{
				System.out.print(j+"\t");
				}
			}
		}
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值