唐诗学习系统
java课程设计:唐诗学习系统
唐诗txt文件链接:https://pan.baidu.com/s/1S80iW341vtFE17tS264Zeg
提取码:k85s
代码实现:
package test02;
import java.io.*;
import java.util.Scanner;
/**
* @author panqiyi
* @date 2020/12/6 - 9:10
*/
public class Test02 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
print(); // 调用打印选择页面方法
String[] read =null; //定义在这里是因为能让选择结构中能调用到
while (true) {
System.out.println("请选择:");
int i = sc.nextInt();
switch (i){
case 1:
read = read(); //返回分割好的字符数组,内容为每一首诗
System.out.println("文件导入完毕!");break;
case 2:
System.out.println("请输入标题:");
String biaoti = sc.next();
title(biaoti,read);break;
case 3:
System.out.println("请输入诗人姓名:");
String name = sc.next();
shiren(name,read);break;
case 4:
System.out.println("请输入诗句:");
String shijv = sc.next();
line(shijv,read);break;
case 5:
randomShi(read);break;
case 6:
export(read);break;
case 0:
System.exit(0);break;
default:
System.out.println("输入错误!");break;
}
}
}
public static void title(String s,String[] shi){ //按标题查诗 方法
System.out.println("查找的诗显示如下:");
for (String s1 : shi) { //遍历每一首诗
String[] line= s1.split("\n"); //将每一首诗分割为每一行,储存到数组
if (line[0].equals(s)){ //数组第一行为标题,比较标题相等则输出对应诗
System.out.println("-----------------------");
System.out.println(s1);
}
}
}
public static void shiren(String s,String[] shi){ //按诗人查诗 方法
int num=1;
System.out.println(s+"--"+"诗词全集");
for (String s1 : shi) { //遍历每一首诗
String[] line = s1.split("\n"); //将每一首诗分割为每一行,储存到数组
String[] author = line[1].split(":"); //将第二行再分割
if (author[1].equals(s)){
System.out.println("-----------------------");//输入字符与诗人比较
System.out.println(num+": "+s1);
num++;
}
}
}
public static void line(String s,String[] shi){ //按诗句查诗 方法
System.out.println("包含 “"+s+"”诗句的唐诗");
for (String s1 : shi) { //遍历每一首诗
String[] line = s1.split("\n"); //将每一首诗分割为每一行,储存到数组
for (String s2 : line) { //遍历每一行
char[] chars = s2.toCharArray();
String shijv = getLine(chars); //调用方法去掉句子末尾的标点符号,返回句子
if (shijv.equals(s)){
System.out.println("-----------------------");
System.out.println(s1);
}
}
}
}
public static void randomShi(String[] shi){ // 猜诗的方法
System.out.println("随机选出的唐诗为:");
System.out.println("-----------------------");
int num=(int)(Math.random()*52); //随机生成 [0~52) 诗一共52首,
String[] hang = shi[num].split("\n"); //将每一首诗分割为每一行,储存到数组
for (int i = 2; i < hang.length; i++) { //输出 诗句内容
System.out.println(hang[i]);
}
System.out.println("-----------------------");
System.out.println("猜一猜这首诗的作者是谁呢?");
Scanner sc = new Scanner(System.in);
String zz = sc.next();
String[] author = hang[1].split(":"); //将第二行再分割
if (author[1].equals(zz)){
System.out.println("恭喜你,猜对了!!");
}else {
System.out.println("很遗憾,猜错了!诗的作者是: "+author[1]);
}
}
public static void export(String[] shi) throws IOException { //按作者导出对应诗储存到本地 方法
for (String s1 : shi) { //遍历每一首诗
String[] line = s1.split("\n"); //将每一首诗分割为每一行,储存到数组
String[] zz = line[1].split(":"); // zz[1]是用来做文件名的
File file = new File("C:\\Users\\panqiyi\\Desktop\\临时\\java课程设计\\诗人\\" + zz[1] + ".txt");
if (!file.exists()){ //判断文件是否存在,不存在就创建
file.createNewFile();
}
}
File file = new File("C:\\Users\\panqiyi\\Desktop\\临时\\java课程设计\\诗人");
String[] list = file.list();
for (String s : list) { //遍历文件夹对象下的文件
String[] stext = s.split("\\."); // 如 李白.txt 按照 "." 分割储存到数组
// System.out.println(stext[0]);
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\panqiyi\\Desktop\\临时\\java课程设计\\诗人\\" + s));
for (String s1 : shi) { //遍历每一首诗
String[] hang = s1.split("\n"); //分割成行
String[] zz = hang[1].split(":");//第二行再分割
if (zz[1].equals(stext[0])){ // 取出诗人与文件名比较,相同则写入
bw.write(s1+"\n");
bw.newLine();
bw.flush();
}
}
bw.close();
}
System.out.println("操作成功!已按作者导出诗集");
}
public static String[] read() throws IOException { //读取唐诗文件 方法
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\panqiyi\\Desktop\\临时\\java课程设计\\唐诗三百首.txt"));
char[] ch = new char[1024];
int len;
StringBuilder sb = new StringBuilder();
while ((len=br.read(ch))!=-1){
sb.append((new String(ch,0,len))); //储存到字符串对象
}
String s = sb.toString();
String[] split = s.split("\n\n"); //每一首诗分开
// System.out.println(split[51]);
return split;
}
public static String getLine(char[] chars){ //去掉每个句子最后的标点符号 方法
char[] arrs = new char[chars.length - 1];
for (int i = 0; i < chars.length-1; i++) { //遍历到数组到倒数第二个,最后一个是标点符号
arrs[i]=chars[i];
}
String s = String.valueOf(arrs); //将没有标点符号的数组转换为 字符串
return s;
}
public static void print(){
System.out.println(" 一起学唐诗! ");
System.out.println("-------------------------------");
System.out.println("-------------------------------");
System.out.println("~~~ 请按1:读取唐诗文件 ~~~");
System.out.println("~~~ 请按2:按标题查唐诗 ~~~");
System.out.println("~~~ 请按3:按诗人查唐诗 ~~~");
System.out.println("~~~ 请按4:按诗句查唐诗 ~~~");
System.out.println("~~~ 请按5:看唐诗猜诗人 ~~~");
System.out.println("~~~ 请按6:按作者导出文件 ~~~");
System.out.println("~~~ 请按0:退出本次学习 ~~~");
System.out.println("-------------------------------");
System.out.println("-------------------------------");
}
}
运行效果
如:
步骤6的按诗人存储诗的结果