用java写一个程序 编写一个解密英文藏头诗,每句一行,小于20行,以#作为藏头诗的输入结束标志,取出每句的第一个字符,连接在一起形成一个字符串并输出。
时间: 2023-06-09 14:02:49 浏览: 374
藏头诗的第一个字母开始,每个字母表示该句第一个单词的首字母,解密后输出诗句。
实现思路:
1. 读取经过加密的藏头诗;
2. 将每一行按照空格分割成单词;
3. 读取每个单词的首字母并且按照顺序恢复成一句诗句;
4. 输出解密后的诗句。
Java代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class HeadPoemDecrypter {
public static void main(String[] args) {
String filePath = "head_poem.txt";
File file = new File(filePath);
StringBuilder decryptionResult = new StringBuilder();
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] words = line.split(" ");
for (String word : words) {
char firstChar = word.charAt(0);
decryptionResult.append(firstChar);
}
decryptionResult.append("\n");
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(decryptionResult.toString());
}
}
说明:
本程序假设输入的文件中包含每行不超过20个单词的藏头诗,每个单词包含至少一个英文字母,藏头诗共多于一行。
运行结果:
若输入以下加密后的藏头诗:
Gude xikred iph ifu kix eaeh,
Unit pa te tubo, eo de,
Tro we hihid od ne him rih,
Heer gi eac, gie eaeh ti nie.
则解密后的诗句为:
GUIKIT, PETEO, TWONHR, HGEGT.
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)