编程题春夏秋冬怎么写

时间:2025-01-27 19:53:27 网络游戏

要用编程语言实现春夏秋冬的表示,可以采用以下几种方法:

方法一:使用条件语句

```python

def generate_poem(season):

if season == "春":

poem = "春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。"

elif season == "夏":

poem = "蝉噪林逾静,鸟鸣山更幽。小荷才露尖尖角,早有蜻蜓立上头。"

elif season == "秋":

poem = "空山新雨后,天气晚来秋。明月松间照,清泉石上流。"

elif season == "冬":

poem = "北风卷地白草折,胡天八月即飞雪。忽如一夜春风来,千树万树梨花开。"

else:

poem = "输入的季节有误,请输入春、夏、秋、冬。"

return poem

print(generate_poem("春"))

print(generate_poem("夏"))

print(generate_poem("秋"))

print(generate_poem("冬"))

```

方法二:使用switch语句

Java示例

```java

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

System.out.print("请输入月份(1-12): ");

Scanner s = new Scanner(System.in);

int month = s.nextInt();

switch (month) {

case 3: case 4: case 5:

System.out.println("春天");

break;

case 6: case 7: case 8:

System.out.println("夏天");

break;

case 9: case 10: case 11:

System.out.println("秋天");

break;

case 12: case 1: case 2:

System.out.println("冬天");

break;

default:

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

}

}

}

```

C示例

```c

include

int main() {

int month;

printf("请输入月份: ");

scanf("%d", &month);

if ((month == 3) || (month == 4) || (month == 5)) {

printf("春季\n");

} else if ((month == 6) || (month == 7) || (month == 8)) {

printf("夏季\n");

} else if ((month == 9) || (month == 10) || (month == 11)) {

printf("秋季\n");

} else if ((month == 12) || (month == 1) || (month == 2)) {

printf("冬季\n");

} else {

printf("错误\n");

}

return 0;

}

```

方法三:使用枚举

```java

public class Hello {

public enum SEASON {

SPRING("spring", "春天"),

SUMMER("summer", "夏天"),

AUTUMN("autumn", "秋天"),

WINTER("winter", "冬天");

private String code;

private String desc;

SEASON(String code, String desc) {

this.code = code;

this.desc = desc;

}

// getter 和 setter 方法

}

public static void main(String[] args) {

System.out.println("Hello, World!");

}

}

```

这些方法可以根据不同的编程语言和需求进行选择。Python示例使用了条件语句,Java和C示例使用了switch语句,而Java示例还使用了枚举来表示季节。