解题思路:1.先定义三个整形变量来接收(n)年,(y)月,(r)日。再定义一个变量来存储总天数。 2.在定义一个数组来储存每月有多少天,数组长度为12。 3.用if判断是否为闰年,如果为闰年让2月=29天,平年28天。 4.然后用if判断月份,因为这个月份还没过完所以要(y)月份-1, 5.最后再加上本月的第几天(r)。 是不是很简单呢! 参考代码: import java.util.Scanner;nyry public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 定义三个整形变量来接收年,月,日。 int n = sc.nextInt(); int y = sc.nextInt(); int r = sc.nextInt(); // 定义一个变量来储存总天数。 int sum = 0; // 定义一个数组来储存每月有多少天。 int[] arr = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31}; // 判断是否为闰年,如果为闰年让2月=29天。 if (n % 400 == 0 || (n % 4 == 0 && n % 100 != 0)){ arr[1] = 29; } // 因为这个月份还没有过完,所以不用算进去。 for (int i = 0; i < y-1; i++) { sum += arr[i]; } // 最后再加上本月的第几天。 System.out.println((sum+r)); } }
0.0分
4 人评分