选择天数程序怎么编程

时间:2025-01-25 12:20:52 网络游戏

选择天数程序可以通过多种编程语言实现,以下是几种常见编程语言的实现方法:

1. C语言

```c

include

int main() {

int year, month, day, T;

printf("请输入年, 月, 日 \n");

scanf("%d,%d,%d", &year, &month, &day);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

T = 31 + 29;

} else {

T = 31 + 28;

}

switch (month) {

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

printf("%d天\n", T + day);

break;

case 4: case 6: case 9: case 11:

printf("%d天\n", T + day);

break;

default:

printf("月份输入错误!\n");

break;

}

return 0;

}

```

2. C++

```cpp

include

using namespace std;

int main() {

int year, month, day;

cout << "输入年份: ";

cin >> year;

cout << "输入月份: ";

cin >> month;

cout << "输入日期: ";

cin >> day;

int T = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? 31 + 29 : 31 + 28;

switch (month) {

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

cout << T + day << "天" << endl;

break;

case 4: case 6: case 9: case 11:

cout << T + day << "天" << endl;

break;

default:

cout << "月份输入错误!" << endl;

break;

}

return 0;

}

```

3. Python

```python

from datetime import datetime

def calculate_days(start_date, end_date):

start_date = datetime.strptime(start_date, "%Y-%m-%d")

end_date = datetime.strptime(end_date, "%Y-%m-%d")

delta = end_date - start_date

return delta.days

start = input("请输入起始日期(YYYY-MM-DD): ")

end = input("请输入结束日期(YYYY-MM-DD): ")

days = calculate_days(start, end)

print("两个日期之间的天数差为:", days)

```

4. Java

```java

import java.util.Scanner;

public class DaysCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("输入年份: ");

int year = scanner.nextInt();

System.out.println("输入月份: ");

int month = scanner.nextInt();

System.out.println("输入日期: ");

int day = scanner.nextInt();

int T = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? 31 + 29 : 31 + 28;

int days;

switch (month) {

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

days = T + day;

break;

case 4: case 6: case 9: case 11:

days = T + day;

break;

default:

System.out.println("月份输入错误!");

return;

}

System.out.println(year + "年" + month + "月" + day + "日是该年的第" + days + "天");

}

}

```

这些代码示例分别展示了如何在C、C++、Python和Java