输入生日的时间,计算距离生日的天数,可以输入多个人的生日,程序可以判断出谁的生日距现在最近。
其中现在的时间可以修改。
// 条件执行与不确定循环
package class_experiment_questions;
import java.util.Scanner;
public class birthday_countdown {
public static void main(String[] args)
{
System.out.println("This programe compares two birthdays and displays which one is sonner.");
System.out.println("Today is 2022/3/5,day #64 of the year.");
System.out.println("Please enter the first person's month and date of birth,separate with space or line breaks.");
int dayofyear1=DayOfYear();
System.out.println("Please enter the second person's month and date of birth,separate with space or line breaks.");
int dayofyear2=DayOfYear();
if(dayofyear1<dayofyear2)
{
System.out.println("the first person's birthday is sonner");
}
else if (dayofyear1>dayofyear2)
{
System.out.println("the second person's birthday is sonner");
}
else
{
System.out.println("What a coincidence that your birthday is on the same day!");
}
}
public static int DayOfYear() // 计算用户输入日期是一年中的第几天
{
Scanner birthday = new Scanner(System.in);
int mouth = birthday.nextInt();
int day = birthday.nextInt();
int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
for(int i=0;i<mouth-1;i++)
{
day += days[i];
}
if(day>=64)
{
Double time_percent = (day-64)/3.65;
System.out.println("Your next birthday is in "+day+" day(s).");
System.out.println("That is "+time_percent+" percent of a year away.");
return day;
}
else
{
int time = 365+day;
System.out.println("Your birthday in 2022 has passed!");
System.out.println("You should still wait"+time+"days for your next birthday.");
return time;
}
}
}
版权声明:本文为weixin_63289399原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。