Java实现输出横版和竖版的古诗 锄禾日当午, 汗滴禾下土。 谁知盘中餐, 粒粒皆辛苦。
时间: 2024-06-01 19:13:07 浏览: 22
可以使用Java中的字符串拼接和循环来实现输出横版和竖版的古诗,示例代码如下:
// 横版输出
System.out.println("锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。");
// 竖版输出
String poem = "锄禾日当午,\n汗滴禾下土。\n谁知盘中餐,\n粒粒皆辛苦。";
for (int i = 0; i < poem.length(); i++) {
System.out.println(poem.charAt(i));
}
希望能够帮助到您。
相关问题
python在列表中存放一首古诗,然后分别用横版和竖版两种形式输出。python
可以先将古诗的每一行作为列表中的一个元素,然后分别输出。
横版输出:
```
poem = ['静夜思', '床前明月光,', '疑是地上霜。', '举头望明月,', '低头思故乡。']
for line in poem:
print(line)
```
竖版输出:
```
poem = ['静夜思', '床前明月光,', '疑是地上霜。', '举头望明月,', '低头思故乡。']
for i in range(len(poem[0])):
for j in range(len(poem)):
print(poem[j][i], end=' ')
print()
```
输出结果如下:
横版输出:
```
静夜思
床前明月光,
疑是地上霜。
举头望明月,
低头思故乡。
```
竖版输出:
```
静 床 疑 举 低
夜 前 是 头 头
思 明 地 望 思
月 上 明 故
霜 月 乡
```
任务6:编写代码实现用户输入的诗句,分别用横版和竖版两种形式输出。
非常感谢您的问题。以下是我的回答:
横版输出:
#include <stdio.h>
#include <string.h>
int main()
{
char poem[100];
printf("请输入您的诗句:\n");
fgets(poem, 100, stdin);
int len = strlen(poem);
for(int i=; i<len; i++)
{
printf("%c ", poem[i]);
}
return ;
}
竖版输出:
#include <stdio.h>
#include <string.h>
int main()
{
char poem[100];
printf("请输入您的诗句:\n");
fgets(poem, 100, stdin);
int len = strlen(poem);
for(int i=; i<len; i++)
{
printf("%c\n", poem[i]);
}
return ;
}