【问题标题】:Java find number of days until your birthdayJava查找距离您生日的天数
【发布时间】:2015-10-23 00:45:26
【问题描述】:

这里有一个计算器可以计算你的生日还有多少天,但是当你输入今天之前的日期时,计算器不会给你正确的答案。我不能使用 import.calender,那么我该如何解决这个问题,以便如果生日在输入的日期之前,它会给我正确的天数?

import java.util.Scanner;

public class Birthdays {
public static void main(String[] args) {

    Scanner newscanner = new Scanner(System.in);
    System.out.print("Please enter today's date (month day): ");
    int z = newscanner.nextInt();
    int y = newscanner.nextInt();
    System.out.println("Today is " + z + "/" + y + "/2016, day #" + absoluteDay(z, y) + " of the year");
    System.out.println();

    System.out.print("Please enter person #1's birthday (month day): ");
    int j = newscanner.nextInt();
    int k = newscanner.nextInt();
    System.out.println(j + "/" + k + "/2016. Your next birthday is in "
            + (Math.abs(absoluteDay(z, y) - absoluteDaytwo(j, k))) + " day(s)");
    System.out.println();

    System.out.print("Please enter person #2's birthday (month day): ");
    int q = newscanner.nextInt();
    int w = newscanner.nextInt();
    System.out.println(q + "/" + w + "/2016. Your next birthday is in "
            + (Math.abs(absoluteDay(z, y) - absoluteDaythree(q, w))) + " day(s)");

    if (j + k > q + w) {
        System.out.print("Person #1's birthday is sooner.");
    } else {
        System.out.print("Person #2's birthday is sooner.");
    }
}

public static int absoluteDay(int z, int y) {
    if (z == 1)
        return y;
    if (z == 2)
        return y + 31;
    if (z == 3)
        return y + 60;
    if (z == 4)
        return y + 91;
    if (z == 5)
        return y + 121;
    if (z == 6)
        return y + 152;
    if (z == 7)
        return y + 182;
    if (z == 8)
        return y + 213;
    if (z == 9)
        return y + 244;
    if (z == 10)
        return y + 274;
    if (z == 11)
        return y + 305;
    if (z == 12)
        return y + 335;
    else
        return 0;
}

public static int absoluteDaytwo(int q, int w) {
    if (q == 1)
        return w;
    if (q == 2)
        return w + 31;
    if (q == 3)
        return w + 60;
    if (q == 4)
        return w + 91;
    if (q == 5)
        return w + 121;
    if (q == 6)
        return w + 152;
    if (q == 7)
        return w + 182;
    if (q == 8)
        return w + 213;
    if (q == 9)
        return w + 244;
    if (q == 10)
        return w + 274;
    if (q == 11)
        return w + 305;
    if (q == 12)
        return w + 335;
    else
        return 0;
}

public static int absoluteDaythree(int j, int k) {
    if (j == 1)
        return k;
    if (j == 2)
        return k + 31;
    if (j == 3)
        return k + 60;
    if (j == 4)
        return k + 91;
    if (j == 5)
        return k + 121;
    if (j == 6)
        return k + 152;
    if (j == 7)
        return k + 182;
    if (j == 8)
        return k + 213;
    if (j == 9)
        return k + 244;
    if (j == 10)
        return k + 274;
    if (j == 11)
        return k + 305;
    if (j == 12)
        return k + 335;
    else
        return 0;
}

}

【问题讨论】:

    标签: java calculator


    【解决方案1】:

    关键点很简单,

    if (today > birthday) then nextBirthday = 365 - (today-birthday)

    完整代码如下:

    /**
     * Created by chenzhongpu on 23/10/2015.
     */
    import java.util.Scanner;
    
    public class Birthdays {
        private static int DAYS_YEAR = 365;
        public static void main(String[] args) {
    
            Scanner scanner = new Scanner(System.in);
            System.out.print("Please enter today's date (month day): ");
            int z = scanner.nextInt();
            int y = scanner.nextInt();
            int currentDays = absoluteDay(z, y);
            System.out.println("Today is " + z + "/" + y + "/2016, day #" + currentDays + " of the year");
    
            System.out.print("Please enter person #1's birthday (month day): ");
            int j = scanner.nextInt();
            int k = scanner.nextInt();
            int birthDayDays1 = absoluteDay(j, k);
            int nextBirthdays1 =
                    birthDayDays1-currentDays >= 0 ? birthDayDays1-currentDays: DAYS_YEAR - (currentDays-birthDayDays1);
            System.out.println(j + "/" + k + "/2016. Your next birthday is in "
                    + nextBirthdays1 + " day(s)");
    
            System.out.print("Please enter person #2's birthday (month day): ");
            int q = scanner.nextInt();
            int w = scanner.nextInt();
            int birthDayDays2 = absoluteDay(q, w);
            int nextBirthdays2 =
                    birthDayDays2-currentDays >= 0 ? birthDayDays2-currentDays: DAYS_YEAR - (currentDays-birthDayDays2);
            System.out.println(q + "/" + w + "/2016. Your next birthday is in "
                    + nextBirthdays2 + " day(s)");
    
            if(nextBirthdays1 > nextBirthdays2){
                System.out.println("#2 is sooner");
            }else if(nextBirthdays1 < nextBirthdays2){
                System.out.println("#1 is sooner");
            }else{
                System.out.println("equal");
            }
    
        }
    
        private static int absoluteDay(int month, int day){
            int[] days = {0, 0, 31, 60, 91, 121, 91, 121, 152, 182,
            213, 244, 274, 305, 335};
            return days[month] + day;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2013-11-10
      • 1970-01-01
      相关资源
      最近更新 更多