注册 登录
编程论坛 JAVA论坛

问题:输出每月第一天是星期几

bug娃娃 发布于 2019-05-11 16:08, 4574 次点击
//显示每月的第一天是星期几
public class homework{
    public     static void main(String[] args) {
            //create a Scanner
            Scanner input = new Scanner(System.in);
            //prompt the user to enter the year and month
            System.out.print("Please input the year:");
            int year = input.nextInt();
            //What day is the first day?
            System.out.print("Please input the first day is:");
            int firstDay = input.nextInt();
            switch(firstDay) {
                    case 0:System.out.println("The first day is " + "Sunday");break;
                    case 1:System.out.println("The first day is " + "Monday");break;
                    case 2:System.out.println("The first day is " + "Tuesday");break;
                    case 3:System.out.println("The first day is " + "Wednesday");break;
                    case 4:System.out.println("The first day is " + "Thursday");break;
                    case 5:System.out.println("The first day is " + "Friday");break;
                    case 6:System.out.println("The first day is " + "Saturday");break;                        
           }            
            int day1 = 0,day2 = 0;
            int[] run= {31,29,31,30,31,30,31,31,30,31,30,31};;
                        
            int[] ping = {31,28,31,30,31,30,31,31,30,31,30,31};   
            int Day1 = 0,Day2 = 0;
            int i = 0,k = 0,temp = 1;
            while(temp <= 12) {
            if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            
                 Day1 += run[i] + 1;
                 day1 = Day1 % 7;
                 switch(day1) {
                       case 0:System.out.println("The month of first day is " + "Sunday");break;
                       case 1:System.out.println("The month of first day is " + "Monday");break;
                       case 2:System.out.println("The month of first day is " + "Tuesday");break;
                       case 3:System.out.println("The month of first day is " + "Wednesday");break;
                       case 4:System.out.println("The month of first day is " + "Thursday");break;
                       case 5:System.out.println("The month of first day is " + "Friday");break;
                       case 6:System.out.println("The month of first day is " + "Saturday");break;                        
                 }
                 i++;                 
            }
            else {
                 Day2 += ping[k] + 1;
                 day2 = Day2 % 7;
                 switch(day1) {
                       case 0:System.out.println("The month of first day is " + "Sunday");break;
                       case 1:System.out.println("The month of first day is " + "Monday");break;
                       case 2:System.out.println("The month of first day is " + "Tuesday");break;
                       case 3:System.out.println("The month of first day is " + "Wednesday");break;
                       case 4:System.out.println("The month of first day is " + "Thursday");break;
                       case 5:System.out.println("The month of first day is " + "Friday");break;
                       case 6:System.out.println("The month of first day is " + "Saturday");break;               
                 }
                 k++;        
             }
            temp++;
        }
            input.close();
        }
}
我输出来的结果不正确,是一些重复的星期
求解答
13 回复
#2
不懂才问2019-05-11 21:32
import java.util.Scanner;
public class TestJava{
    public     static void main(String[] args) {
            //create a Scanner
            Scanner input = new Scanner(System.in);
            //prompt the user to enter the year and month
            System.out.print("Please input the year:");
            int year = input.nextInt();
            //What day is the first day?
            System.out.print("Please input the first day is:");
            int firstDay = input.nextInt();
            switch(firstDay) {
                    case 0:System.out.println("The first day is " + "Sunday");break;
                    case 1:System.out.println("The first day is " + "Monday");break;
                    case 2:System.out.println("The first day is " + "Tuesday");break;
                    case 3:System.out.println("The first day is " + "Wednesday");break;
                    case 4:System.out.println("The first day is " + "Thursday");break;
                    case 5:System.out.println("The first day is " + "Friday");break;
                    case 6:System.out.println("The first day is " + "Saturday");break;                        
           }            
            int day1 = 0,day2 = 0;
            int[] run= {31,29,31,30,31,30,31,31,30,31,30,31};
                        
            int[] ping = {31,28,31,30,31,30,31,31,30,31,30,31};   
            int Day1 = 0,Day2 = 0;
            int i = 0,k = 0,temp = 1;
            while(temp <= 12) {
            if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            
                 Day1 += run[i] + 1;//这里为啥要加1呢?没太理解。
                 System.out.print("Day1目前的值是" + Day1);
                 day1 = Day1 % 7;
                 System.out.print("day1目前的值是" + day1);
                 switch(day1) {
                       case 0:System.out.println("The month of first day is " + "Sunday");break;
                       case 1:System.out.println("The month of first day is " + "Monday");break;
                       case 2:System.out.println("The month of first day is " + "Tuesday");break;
                       case 3:System.out.println("The month of first day is " + "Wednesday");break;
                       case 4:System.out.println("The month of first day is " + "Thursday");break;
                       case 5:System.out.println("The month of first day is " + "Friday");break;
                       case 6:System.out.println("The month of first day is " + "Saturday");break;                        
                 }
                 i++;                 
            }
            else {
                 Day2 += ping[k] + 1;
                 System.out.print("Day2目前的值是" + Day2);
                 day2 = Day2 % 7;
                 System.out.print("day2目前的值是" + day2);
                 switch(day2) {
                       case 0:System.out.println("The month of first day is " + "Sunday");break;
                       case 1:System.out.println("The month of first day is " + "Monday");break;
                       case 2:System.out.println("The month of first day is " + "Tuesday");break;
                       case 3:System.out.println("The month of first day is " + "Wednesday");break;
                       case 4:System.out.println("The month of first day is " + "Thursday");break;
                       case 5:System.out.println("The month of first day is " + "Friday");break;
                       case 6:System.out.println("The month of first day is " + "Saturday");break;               
                 }
                 k++;        
             }
            temp++;
        }
            input.close();
        }
}
#3
林月儿2019-05-11 22:02
程序代码:
import java.time.LocalDate;
import java.util.Scanner;

public class Test0511 {
    // //显示每月的第一天是星期几
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please input the year:");
        int year = scanner.nextInt();
        scanner.close();
        LocalDate date = LocalDate.of(year, 1, 1);
        for(int i=0; i<12; i++) {
            System.out.println(date+":"+date.getDayOfWeek());
            date = date.plusMonths(1);
        }
    }
}

Please input the year:2019
2019-01-01:TUESDAY
2019-02-01:FRIDAY
2019-03-01:FRIDAY
2019-04-01:MONDAY
2019-05-01:WEDNESDAY
2019-06-01:SATURDAY
2019-07-01:MONDAY
2019-08-01:THURSDAY
2019-09-01:SUNDAY
2019-10-01:TUESDAY
2019-11-01:FRIDAY
2019-12-01:SUNDAY
#4
林月儿2019-05-11 22:45
程序代码:
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Test0511 {
    //显示每月的第一天是星期几
    public static void main(String[] args) {
        // 绘制弹窗及控件
        JFrame jframe = new JFrame();
        JTextField jtf = new JTextField(20);
        JLabel lbl = new JLabel("请输入年份:");
        JPanel topPanel = new JPanel();
        topPanel.add(lbl);
        topPanel.add(jtf);
        jframe.add(topPanel, BorderLayout.NORTH);
        JTextArea jta = new JTextArea();
        jframe.add(jta, BorderLayout.CENTER);
        jframe.setVisible(true);
        jframe.setSize(640, 320);
        jframe.setTitle("显示每月的第一天是星期几");
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 绑定输入框的键盘输入事件
        jtf.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                String inputVal = jtf.getText();
                if(inputVal.matches("\\d{4}")) {
                    int year = Integer.parseInt(inputVal);
                    showRes(year, jta);
                }
            }
        });
    }
    // 计算并显示
    private static void showRes(int year, JTextArea jta) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, 1, 1);
        StringBuilder stringBuilder = new StringBuilder();
        for(int i=0; i<12; i++) {
            stringBuilder.append(String.format("%tc\n", cal));
            cal.add(Calendar.MONTH, 1);
        }
        jta.setText(stringBuilder.toString());
    }
}


只有本站会员才能查看附件,请 登录
#5
bug娃娃2019-05-12 10:00
我在纸上数的,如果要去下一个月的第一天,就加一
#6
bug娃娃2019-05-12 10:00
回复 3楼 林月儿
你这玩意,好高级啊
#7
bug娃娃2019-05-12 10:02
回复 2楼 不懂才问
你这个程序,有的地方我有点看不懂,可能是我还没有学到那个知识点吧,不过还是谢谢了
#8
林月儿2019-05-12 11:14
回复 6楼 bug娃娃
还好吧,想学我教你啊
#9
bug娃娃2019-05-12 14:58
回复 7楼 bug娃娃
可以,可以,我就想要个师傅呢
#10
larry6062019-05-15 14:24
FORTRAN
#11
bug娃娃2019-05-16 07:20
回复 10楼 larry606
请问那是什么意思?
#12
黄雀0012019-05-26 11:00
回复 3楼 林月儿
崇拜
#13
黄雀0012019-05-26 11:01
回复 3楼 林月儿
崇拜
#14
林月儿2019-05-26 12:56
回复 13楼 黄雀001
过誉
1