Java程序设计编程

使用键盘录入技术,让用户输入年份和该年第一天是星期几,用0、1、2、3、4、5、6分别表示星期日、星期一、星期二、星期三、星期四、星期五、星期六,例如2023年的第一天是星期日,则用户依次输入2023和0,然后就可以在控制台打印出2023年各个月份的第一天是星期几。
提示和要求:
1.在统计2月的天数时,需要注意区分闰年和平年;
2.得到结果后,对照电脑上的日历检查一下结果是否正确。
具体思路怎么弄

望采纳,不懂可以私聊。

img

img

img

import java.util.Scanner;

public class Test06 {
    public static void main(String[] args) {
        int year = 2023;
        int day = 0;
        final Scanner scanner = new Scanner(System.in);
        System.out.println("请输入年");
        year = scanner.nextInt();
        System.out.println("该年第一天是星期几");
        day = scanner.nextInt();
        final String[] data = {"日", "一", "二", "三", "四", "五", "六"};
        System.out.println("1月的第一天是:" + data[day]);
        final int[] ints = {31, year % 4 == 0 ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        for (int i = 0; i < 11; i++) {
            day += ints[i] % 7;
            day = day < 7 ? day : day - 7;
            System.out.println(i + 2 + "月的第一天是:" + data[day]);
        }
    }
}

如有帮助,望采纳

year = eval(input("请输入年份:"))
day = eval(input("请输入该年的第一天是星期几:"))
lst1 = ["January","February","March","April","May","June","July","August","September","October","November","December"]

def is_leapyear(year):
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
        return True
    else:
        return False

if is_leapyear(year):
    for j in range(1,13):
        print("      %d,%s"%(year,lst1[j-1]))
        print("Sun Mon Tue Wed Thr Fri Sat ")
        print("    " * day,end = "")

        if j == 1 or j == 3 or j == 5 or j == 7 or j == 8 or j == 10 or j == 12:
            for i in range(1,32):
                print(format(str(i),"4s"),end = "")
                if (i + day) % 7 == 0:
                    print()
            day =(i + day) % 7
            print()
        elif j == 2:
            for k in range(1,30):
                print(format(str(k),"4s"),end = "")
                if (k + day) % 7 == 0:
                    print()
            day =(k + day) % 7
            print()
        else :
            for l in range(1,31):
                print(format(str(l),"4s"),end = "")
                if (l + day) % 7 == 0:
                    print()
            day =(l + day) % 7
            print()
else:
    for a in range(1,13):
        print("      %d,%s"%(year,lst1[a-1]))
        print("Sun Mon Tue Wed Thr Fri Sat ")
        print("    " * day,end = "")

        if a == 1 or a == 3 or a == 5 or a == 7 or a == 8 or a == 10 or a == 12:
            for z in range(1,32):
                print(format(str(z),"4s"),end = "")
                if (z + day) % 7 == 0:
                    print()
            day =(z + day) % 7
            print()
        elif a == 2:
            for x in range(1,29):
                print(format(str(x),"4s"),end = "")
                if (x + day) % 7 == 0:
                    print()
            day =(x + day) % 7
            print()
        else :
            for c in range(1,31):
                print(format(str(c),"4s"),end = "")
                if (c + day) % 7 == 0:
                    print()
            day =(c + day) % 7
            print()

img


import java.util.Scanner;
    public class dome5 {
        public static void main(String[] args) {
            try(Scanner input = new Scanner(System.in);) {
                System.out.print("Enter year and number represented as someday "+
                        "of week (1 - 7): ");
                showDay(input.nextInt(), input.nextInt());
            }
        }
        //显示每月第一天是星期几
        public static void showDay(int year, int day) {
            String[] months = {"一月", "二月", "三月", "四月", "五月", "六月",
                    "七月", "八月", "九月", "十月", "十一月",
                    "十二月"};
            for (int i = 0; i < months.length; i++)
                System.out.printf("%d的%s是%s\n", year,months[i],
                        day(year, i + 1, 1));
        }
        //某天是星期几
        public static String day(int year, int month, int day) {
            String[] days = {"星期日", "星期六", "星期一", "星期二", "星期三",
                    "星期四", "星期五"};
            int m = month,k, j;
            if(month < 1 || month > 12)    //如果月份不合法,抛出异常
                throw new IllegalArgumentException("Error Month: "+month);
            if(day < 1 || day > 31)        //如果天数不合法,抛出异常
                throw new IllegalArgumentException("Error Day: "+day);
            if(month == 1 || month == 2) {    //处理月份为1或2的情况
                m = (month == 1) ? 13 : 14;    //如果月份为1,m = 13;否则,m = 14
                year--;        //年份减一
            }
            k = year % 100;                //世纪的第几年
            j = Math.abs(year / 100);    //世纪数
            int h = (day + (26 * (m + 1)) / 10 + k + (k / 4) + (j / 4) + 5 * j) % 7;
            return days[h];
        }
    }

代码:

package pack2;

import java.util.Scanner;

public class DayIs {

    public static void main(String[] args) {
        try(Scanner input = new Scanner(System.in);) {
            System.out.print("Enter year and number represented as someday "+
                    "of week (0 - 6): ");
            showDay(input.nextInt(), input.nextInt());
        }
    }

    //显示每月第一天是星期几
    public static void showDay(int year, int day) {
        String[] months = {"January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November",
                "December"};
        for (int i = 0; i < months.length; i++)
            System.out.printf("%10s 1, %d is %s\n", months[i], year,
                    day(year, i + 1, 1));
    }

    //某天是星期几
    public static String day(int year, int month, int day) {
        String[] days = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday",
                "Thursday", "Friday"};
        int m = month,k, j;

        if(month < 1 || month > 12)    //如果月份不合法,抛出异常
            throw new IllegalArgumentException("Error Month: "+month);
        if(day < 1 || day > 31)        //如果天数不合法,抛出异常
            throw new IllegalArgumentException("Error Day: "+day);

        if(month == 1 || month == 2) {    //处理月份为1或2的情况
            m = (month == 1) ? 13 : 14;    //如果月份为1,m = 13;否则,m = 14
            year--;        //年份减一
        }
        k = year % 100;                //世纪的第几年
        j = Math.abs(year / 100);    //世纪数

        int h = (day + (26 * (m + 1)) / 10 + k + (k / 4) + (j / 4) + 5 * j) % 7;
        return days[h];
    }
}

Scanner 输入,if判断


package demo;
import java.util.Scanner;
 
public class practice {
        static  int year; 
    public static void main(String[] args) {
        //接收用户的输入
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the year and the date: \n");
        year = input.nextInt();
        int date = input.nextInt();
        //调用函数
        PrintDate(date);
                       
    }
//判断是否闰年
public static boolean IsLeapYear(int year) {
    if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        return true;
    else return false;
}
//返回该月的天数
public static int Days(int month) {
    switch(month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:return 31;
    case 2:
        if(IsLeapYear(year)) {
            return 29;
        }else {
            return 28;
        }
    case 4:
    case 6:
    case 9:
    case 11:return 30;
    default:
       return 0;
    }
}
//打印月历分割线+周英文简写
public static void PrintMonthHead() {
            System.out.print("_________________________________\n");
            System.out.print("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
                    System.out.print("\n");
}
//打印日历
public static void PrintDate(int date) {
    //count表示月历开头的空隔天数
     int count = date == 7 ? 0 : date; 
     for(int i = 1;i < 13;i ++) {
            //打印月历表头
            System.out.print("                      " + i + "月\n"); 
            
            //打印月历分割线+周英文简写
            PrintMonthHead(); 
            
            //打印月历开头的空隔
            if(count != 0)
                for(int j = 0;j < count;j++) 
                    System.out.print("     ");
            
            //打印日历具体内容
            for(int k = 1;k <= Days(i);k ++) {
                if(k < 10) System.out.print( k + "    ");
                if(k >= 10) System.out.print( k + "   ");
                count ++;
                //累计满7天,换行
                if(count % 7 == 0) System.out.print("\n");
            }
            
            //计算下个月的月历开头的空隔天数
            count = ((count - 1) % 7) + 1 == 7 ? 0 : ((count - 1) % 7) + 1;
            //两个月之间换两行
            System.out.print("\n \n");
    }
  }
}