本文介绍了Java查找直到您生日的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个计算器,可以查找您的生日还有多少天,但是当您输入今天日期之前的某天时,计算器将无法为您提供正确的答案.我不能使用import.calender,那么我将如何解决该问题,以便如果生日在输入的日期之前,可以给我正确的天数?

Here is a calculator to find how many more days your birthday is in, but when you enter a day that is before today's date, then the calculator will not give you the correct answer. I cannot use import.calender, so how would I solve the problem so that if the birthday was before the date entered that it would give me the correct number of days?

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;
}

}

推荐答案

关键点很简单,

如果(今天>生日)则nextBirthday = 365-(今天-生日)

这是完整的代码:

/**
 * 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;
    }
}

这篇关于Java查找直到您生日的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 06:07