Java 查找距您生日还有多少天

标签 java calculator

这里有一个计算器可以计算您的生日还有多少天,但是当您输入今天日期之前的一天时,计算器将不会给您正确的答案。我无法使用 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;
}

}

最佳答案

关键点很简单,

if(今天 > 生日)然后 下一个生日 = 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 查找距您生日还有多少天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33293128/

相关文章:

java - 在 ScrollPane 中包含表格和图像的布局

java - 使用从 Activity 传递的数据更新 Fragment UI

javascript - 无需刷新页面即可重置功能

Java GPA 计算器循环

java - 制作一个单弦线计算器

java - 如何将删除前导 0 的字符串转换为日期

java - 从 3GP 文件中提取 AMR 数据

java - 线程 "main"java.lang.NoClassDefFoundError :Helloworld<wrong mname:Helloworld> 中出现此异常的原因是什么

java - java计算中数字和运算符的区分

Powershell 基本版