输入三个整数,分别为年year,月month,日day,以空格隔开,系统保证输入的是合法日期。用c语言
时间: 2024-03-29 22:34:09 浏览: 51
您好!根据您的要求,我为您编写了如下的C语言代码:
```c
#include <stdio.h>
int main()
{
int year, month, day;
printf("请输入年月日,以空格隔开:\n");
scanf("%d %d %d", &year, &month, &day);
printf("您输入的日期是:%d年%d月%d日\n", year, month, day);
return 0;
}
```
您可以将以上代码复制到您的C语言开发环境中运行,然后按照提示输入年月日,程序会输出您输入的日期。如果您有更多的问题,欢迎随时向我提出。
相关问题
输入三个整数,分别为年year,月month,日day,以空格隔开,系统保证输入的是合法日期。输出日期是该年的第几天并换行。用c语言
好的,根据您的要求,我为您编写了如下的C语言代码:
```c
#include <stdio.h>
int main()
{
int year, month, day;
int days = 0;
printf("请输入年月日,以空格隔开:\n");
scanf("%d %d %d", &year, &month, &day);
// 计算该日期是该年的第几天
for(int i = 1; i < month; i++)
{
switch(i)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days += 31;
break;
case 4: case 6: case 9: case 11:
days += 30;
break;
case 2:
// 判断是否为闰年
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
days += 29;
else
days += 28;
break;
}
}
days += day;
printf("%d年%d月%d日是该年的第%d天。\n", year, month, day, days);
return 0;
}
```
以上代码会先提示用户输入年月日,然后根据用户输入的日期计算该日期是该年的第几天,并输出结果。如果您有更多的问题,欢迎随时向我提出。
用java写一个程序有三个输入变量 month、day、year(month 、 day 和 year 均为整数值,并且满足:1≤month≤12 和 1≤day≤31),分别作为输入日期的月份、日、年份,通过程序可以输出该输入日期 在日历上隔一天的日期
以下是用Java编写的程序,实现输入一个日期,输出在日历上隔一天的日期:
```java
import java.util.Scanner;
public class NextDay {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入日期(month day year,用空格分隔):");
int month = input.nextInt();
int day = input.nextInt();
int year = input.nextInt();
int nextMonth = month;
int nextDay = day;
int nextYear = year;
// 判断输入日期是否合法
boolean isValid = true;
if (month < 1 || month > 12 || day < 1 || day > 31) {
isValid = false;
} else {
if (month == 2) {
// 二月份特殊处理
if (isLeapYear(year)) {
if (day > 29) {
isValid = false;
}
} else {
if (day > 28) {
isValid = false;
}
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) {
isValid = false;
}
}
}
if (!isValid) {
System.out.println("输入日期不合法!");
} else {
// 计算隔一天的日期
if (day < getDaysInMonth(month, year)) {
nextDay = day + 1;
} else {
nextDay = 1;
if (month == 12) {
nextMonth = 1;
nextYear = year + 1;
} else {
nextMonth = month + 1;
}
}
// 输出隔一天的日期
System.out.printf("下一天的日期是:%d年%d月%d日", nextYear, nextMonth, nextDay);
}
}
// 判断是否为闰年
public static boolean isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true;
} else {
return false;
}
}
// 获取一个月的天数
public static int getDaysInMonth(int month, int year) {
if (month == 2) {
// 二月份特殊处理
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else {
return 31;
}
}
}
```
运行程序,输入一个日期(如8 31 2021),则输出下一天的日期(即9月1日)。
注意,程序中对输入日期的合法性进行了判断,若输入的月份和日份不在合法范围内,则输出提示信息。此外,程序中还实现了判断闰年和获取一个月的天数的函数,以方便计算隔一天的日期。