计算从计算机上输入的年月日,属于这一年的第几天

小灰灰 2022-06-12 08:18 331阅读 0赞

第一种,使用if-else来判断

  1. import java.util.Scanner;
  2. public class Date{
  3. public static void main(String [] args){
  4. //声明一个整型变量sum ,统计总天数
  5. int sum=0;
  6. //创建new 扫描对象,扫描用户的键盘输入
  7. Scanner input = new Scanner(System.in);
  8. System.out.println("请输入年份:");
  9. //声明一个整数变量year,接收用户输入的年份
  10. int year=input.nextInt();
  11. System.out.println("请输入月份: ");
  12. //声明一个整数变量month,接收用户输入的月份
  13. int month=input.nextInt();
  14. while(month<=0||month>=13){
  15. System.out.println("请重新输入月份");
  16. month=input.nextInt();
  17. }
  18. System.out.println("请输入天数: ");
  19. //声明一个整数变量day,接收用户输入的日期
  20. int day=input.nextInt();
  21. while(day<=0|| day>=32)
  22. {
  23. System.out.println("请重新输入日期");
  24. day=input.nextInt();
  25. }
  26. for(int i=1;i<month;i++)
  27. { if(i==4||i==6||i==9||i==11)
  28. {
  29. sum+=30;
  30. }
  31. else if(i==2&&((year%4==0&&year%100!=0)||(year%400==0)))
  32. {
  33. sum+=29;
  34. }
  35. else if(i==2&&!((year%4==0&&year%100!=0)||(year%400==0)))
  36. {
  37. sum+=28;
  38. }
  39. else{
  40. sum+=31;
  41. }
  42. }
  43. sum+=day;
  44. System.out.println("是这一年的第"+sum+"天");
  45. }
  46. }

第二种,使用switch case来编程,使用正序思想

  1. import java.util.Scanner;
  2. public class DaysOfYear{
  3. public static void main(String[] args){
  4. //声明一个整型变量sum ,统计总天数
  5. int total=0;
  6. //创建new 扫描对象,扫描用户的键盘输入
  7. Scanner input = new Scanner(System.in);
  8. System.out.println("请输入年份:");
  9. //声明一个整数变量year,接收用户输入的年份
  10. int year=input.nextInt();
  11. System.out.println("请输入月份: ");
  12. //声明一个整数变量month,接收用户输入的月份
  13. int month=input.nextInt();
  14. while(month<=0||month>=13){
  15. System.out.println("请重新输入月份");
  16. month=input.nextInt();
  17. }
  18. System.out.println("请输入天数: ");
  19. //声明一个整数变量date,接收用户输入的日期
  20. int date=input.nextInt();
  21. while(date<=0|| date>=32)
  22. {
  23. System.out.println("请重新输入日期");
  24. date=input.nextInt();
  25. }
  26. //但是7月前的1-6月都是完整过完了,迭代输入月份以前的所有月
  27. for(int m=1;m<month;m++)
  28. {//每循环一次,则代表一个月份//任何一个变量再后续使用时,一定要有值//一年中31天的月份更普遍//默认就有31天
  29. int daysOfMonth=31;
  30. //System.out.println(days);
  31. switch(m)
  32. {case 4:
  33. case 6:
  34. case 9:
  35. case 11:
  36. daysOfMonth=30;
  37. break;
  38. case 2:
  39. //2月份普遍28天
  40. daysOfMonth=28;
  41. //只有闰年时才有29天
  42. if(((year%4==0&&year%100!=0)||(year%400==0)))
  43. {//System.out.println(month+"月有29天");daysOfMonth=29;}break;}
  44. //算出每个月有多少天,累加到总天数中
  45. total+=daysOfMonth;}
  46. //循环结束了,则前面几个月天数总和就计算出来了
  47. //再把输入的当月天数累加进去
  48. total+=date;
  49. System.out.println(total);}}
  50. 第三种,使用switch-case来编程,使用倒序思想
  51. import java.util.Scanner;
  52. public class DaysOfYearU1{
  53. public static void main(String[] args){
  54. //声明一个整型变量sum ,统计总天数
  55. int total=0;
  56. //创建new 扫描对象,扫描用户的键盘输入
  57. Scanner input = new Scanner(System.in);
  58. System.out.println("请输入年份:");
  59. //声明一个整数变量year,接收用户输入的年份
  60. int year=input.nextInt();
  61. System.out.println("请输入月份: ");
  62. //声明一个整数变量month,接收用户输入的月份
  63. int month=input.nextInt();
  64. while(month<=0||month>=13){
  65. System.out.println("请重新输入月份");
  66. month=input.nextInt();
  67. }
  68. System.out.println("请输入天数: ");
  69. //声明一个整数变量date,接收用户输入的日期
  70. int date=input.nextInt();
  71. while(date<=0|| date>=32)
  72. {
  73. System.out.println("请重新输入日期");
  74. date=input.nextInt();
  75. } //但是7月前的1-6月都是完整过完了,迭代输入月份以前的所有月switch(month-1){case 11:total+=30;case 10:total+=31;case 9:total+=30;case 8:total+=31;case 7:total+=31;case 6:total+=30;case 5:total+=31;case 4:total+=30;case 3:total+=31;case 2://2月份普遍28天int daysOfMonth=28;//只有闰年时才有29天if(((year%4==0&&year%100!=0)||(year%400==0))){//System.out.println(month+"月有29天");daysOfMonth=29;}total+=daysOfMonth;case
  76. 1:total+=31;}//循环结束了,则前面几个月天数总和就计算出来了//再把输入的当月天数累加进去total+=date;System.out.println(total);}}

发表评论

表情:
评论列表 (有 0 条评论,331人围观)

还没有评论,来说两句吧...

相关阅读