在java中怎么用循环实现:“输入某年某月某日,判断这一天是这一年的第几天?”

2024-10-28 15:06:57
推荐回答(1个)
回答1:

输入判断什么的就不做了 import java.util.Scanner; public class test2 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int year, month, day; int allDay; int []days1 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int []days2 = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; while (true) { allDay = 0; year = 0; month = 0; day = 0; System.out.println("请输入年份"); year = s.nextInt(); System.out.println("请输入月份"); month = s.nextInt(); System.out.println("请输入日期"); day = s.nextInt(); if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { for (int i = 0; i < month - 1; i++) allDay += days2[i]; } else { for (int i = 0; i < month - 1; i++) allDay += days1[i]; } allDay += day; System.out.println("这一天是这一年的第" + allDay + "天"); } } }