当用户从365输入数字时,如何打印出sprcific日期 [英] How do I print out a sprcific date when the user inputs a number from 365

查看:129
本文介绍了当用户从365输入数字时,如何打印出sprcific日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个名为DayFunctions的程序,其中包含以下方法。它应该有一个main,允许你用来自用户的输入值调用这两个方法



String getMonthDay(int dayNumber)

此方法将年份中的日期编号作为参数。假设它不是闰年。返回的字符串可以采用格式1/1或1月1日



int getDayNumer(int month,int day)

返回年份的dayNumber。例如,1月1日是1,1月31日是31。

这是对齐



我尝试了什么:



Write a program called DayFunctions that includes the methods below. It should have a main that lets you call these two methods with values input from the user

String getMonthDay(int dayNumber)
This method takes as a parameter the day number in the year. Assume that it is not a leap year. The String returned can be either in the format "1/1" or "January 1"

int getDayNumer(int month, int day)
This returns the dayNumber for the year. For example, January 1 is 1 and January 31 is 31.
this is the asignment

What I have tried:

import java.util.Scanner;
public class DayNumbers {
    private static int[] firstDayNumbers = new int[] {-1,1,32,60,91,121,152,182,213,244,274,305,335            
    };
    private static int getDayNumber(int month,int day) {
    
        int x = firstDayNumbers[month];  
         x = x+day-1;
         return x;
        }
    public static String getMonthDay(int dayNumber) {
    
        String[] months = {"jaunuary", "Febuary", "March", "April", "May", "June", "july", "August", "September", "October", "November", "December"}; 
        
        String i=null;
        if (dayNumber <= 31) {
            i = months[0];
        } else if (dayNumber <= 59 ) {
            i = months[1];
        }else if (dayNumber <= 90) {
            i = months[2];
        }else if (dayNumber <= 120) {
            i = months[3];
        }else if (dayNumber <= 151 ) {
            i = months[4];
        }else if (dayNumber <= 181) {
            i = months[5];
        }else if (dayNumber <= 212) {
            i = months[6];
        }else if (dayNumber <= 243) {
            i = months[7];
        }else if (dayNumber <= 273 ) {
            i = months[8];
        }else if (dayNumber <= 304) {
            i = months[9];
        }else if (dayNumber <= 334) {
            i =months[10];
        }
        String day = null;
        int m = 0;
    
        
        if (m == 2) {
            day += 31;
        } else if (m == 3) {
            day += 59;
        } else if (m == 4) {
            day += 90;
        } else if (m == 5) {
            day += 31 + 28 + 31 + 30;
        } else if (m == 6) {
            day += 31 + 28 + 31 + 30 + 31;
        } else if (m == 7) {
            day += 31 + 28 + 31 + 30 + 31 + 30;
        } else if (m == 8) {
            day += 31 + 28 + 31 + 30 + 31 + 30 + 31;
        } else if (m == 9) {
            day += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
        } else if (m == 10) {
            day += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
        } else if (m == 11) {
            day += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
        } else if (m == 12) {
            day += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
        }
    

    
    }
    public static void main(String[] args) {
        int dayNumber=156;
        String d= getMonthDay(dayNumber);
        System.out.println(""+d);
        
    }

}

推荐答案

1)1月未拼写作为jaunuary

2)getMonthDay不返回值。

3)你在i中生成一个值,然后忽略它。

4)你的如果条件永远不会匹配,因为你在开始之前将 m 设置为零。



使用你的数组代替long if测试:查找dayNumber小于的最后一个值的简单循环将告诉你哪个月。作为月份数组的索引,为您提供月份名称,减法将为您提供月份的日期。
1) "January" is not spelled as "jaunuary"
2) getMonthDay does not return a value.
3) You generate a value in i, and then ignore it.
4) Your if conditions will never match, since you are setting m to zero before you start.

Instead of the long if test, use your array: a simple loop looking for the last value that dayNumber is less than will tell you which month. That as an index into the month array gives you the month name, and a subtraction will give you the day of the month.


请阅读 OriginalGriff [ ^ ]的回答非常仔细,想一想!一个月的天数是众所周知的。只有一个月可能会返回28或29天。因此,您必须计算当前年份是否为闰年 [ ^ ]。怎么样?



Please, read OriginalGriff[^] 's answer very carefully and think of it! The number of days in a month is well known. There's only one month which may return 28 or 29 days. So, you have to calculate if current year is a leap year[^]. How?

引用:

算法



以下伪代码确定一年是公历中的闰年还是共同年份(以及1582年前的公历中的公历)。正在测试的年变量是表示公历中年份数的整数,并且测试被安排为首先派遣最常见的案例。应该注意将数学整数可分性转换为特定的编程语言。



Algorithm

The following pseudocode determines whether a year is a leap year or a common year in the Gregorian calendar (and in the proleptic Gregorian calendar before 1582). The year variable being tested is the integer representing the number of the year in the Gregorian calendar, and the tests are arranged to dispatch the most common cases first. Care should be taken in translating mathematical integer divisibility into specific programming languages.

if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)





所以,如果有人打75,那么

a)共同年份:1月31日+ 2月28日+ 3月16日;结果:3月16日

b)闰年,它将是:1月31日+ 2月29日+ 3月15日;结果:3月15日



现在更清楚了吗?



So, if someone will type 75, then
a) for a common year it would be: 31 days of January + 28 days of February + 16 days of March; result: 16 March
b) for a leap year, it would be: 31 days of January + 29 days of February + 15 days of March; result: 15 March

Does it clearer now?


这篇关于当用户从365输入数字时,如何打印出sprcific日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆