| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 259 人关注过本帖
标题:问题:输出每月第一天是星期几
只看楼主 加入收藏
bug娃娃
Rank: 2
等 级:论坛游民
帖 子:75
专家分:10
注 册:2019-3-15
结帖率:93.33%
  已结贴   问题点数:20  回复次数:10   
问题:输出每月第一天是星期几
//显示每月的第一天是星期几
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();
        }
}
我输出来的结果不正确,是一些重复的星期
求解答
2019-05-11 16:08
不懂才问
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:大草原
等 级:贵宾
威 望:29
帖 子:1462
专家分:6553
注 册:2010-7-5
  得分:10 
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();
        }
}

报告老师,我低头不是因为我在装低调,是你问的问题,我真的不会答,,,
2019-05-11 21:32
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:123
帖 子:1870
专家分:8197
注 册:2015-3-19
  得分:10 
程序代码:
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

知命以下无敌
2019-05-11 22:02
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:123
帖 子:1870
专家分:8197
注 册:2015-3-19
  得分:0 
程序代码:
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());
    }
}


附件: 您没有浏览附件的权限,请 登录注册

知命以下无敌
2019-05-11 22:45
bug娃娃
Rank: 2
等 级:论坛游民
帖 子:75
专家分:10
注 册:2019-3-15
  得分:0 
我在纸上数的,如果要去下一个月的第一天,就加一
2019-05-12 10:00
bug娃娃
Rank: 2
等 级:论坛游民
帖 子:75
专家分:10
注 册:2019-3-15
  得分:0 
回复 3楼 林月儿
你这玩意,好高级啊
2019-05-12 10:00
bug娃娃
Rank: 2
等 级:论坛游民
帖 子:75
专家分:10
注 册:2019-3-15
  得分:0 
回复 2楼 不懂才问
你这个程序,有的地方我有点看不懂,可能是我还没有学到那个知识点吧,不过还是谢谢了
2019-05-12 10:02
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:123
帖 子:1870
专家分:8197
注 册:2015-3-19
  得分:0 
回复 6楼 bug娃娃
还好吧,想学我教你啊

知命以下无敌
2019-05-12 11:14
bug娃娃
Rank: 2
等 级:论坛游民
帖 子:75
专家分:10
注 册:2019-3-15
  得分:0 
回复 7楼 bug娃娃
可以,可以,我就想要个师傅呢
2019-05-12 14:58
larry606
Rank: 1
等 级:新手上路
帖 子:4
专家分:4
注 册:2019-5-15
  得分:0 
FORTRAN
2019-05-15 14:24







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.114292 second(s), 8 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved