词语自我听写小程序
博主假期常需辅导弟弟学习,出于偷懒的想法写下了这个小程序,望各路大神多多指教 _(:з)∠)_
1 package text; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.io.FileReader; 8 import java.io.FileWriter; 9 import java.io.IOException; 10 import java.util.ArrayList; 11 import java.util.Arrays; 12 import java.util.List; 13 import java.util.Random; 14 import java.util.Scanner; 15 16 import javax.annotation.processing.FilerException; 17 /*从input1.txt和input.txt文本文档轮流按行读取数据并随机输出,可用于自我听写 18 */ 19 20 public class Main { 21 public static void main(String[] args) { 22 String aString = ".\\input1.txt";//input1.txt放入拼音 23 String bString = ".\\input.txt";//input.txt放入词语 24 25 try { 26 FileReader aFileReader = new FileReader(aString);//字符流用于读/写文件 27 FileReader bFileReader = new FileReader(bString); 28 29 BufferedReader aBufferedReader = new BufferedReader(aFileReader);//包装字符流以提高读写效率 30 BufferedReader bBufferedReader = new BufferedReader(bFileReader); 31 32 String tempString = aBufferedReader.readLine();//按行读取 33 Scanner scanner = new Scanner(System.in); 34 List<String> aList = new ArrayList<String>();//储存拼音 35 List<String> bList = new ArrayList<String>();//储存词语 36 List<Boolean> cList = new ArrayList<Boolean>();//标记词语是否已听写过 37 38 while (tempString != null) {//从文本读入内容到list 39 aList.add(tempString); 40 cList.add(true);//标记该词语未被听写过 41 42 tempString = bBufferedReader.readLine(); 43 bList.add(tempString); 44 tempString = aBufferedReader.readLine(); 45 } 46 47 Random random = new Random(); 48 int id; 49 for (int i=0; i<Math.min(aList.size(), 10); ) {//开始听写,我这里设置听写个数不超过10 50 id = random.nextInt(aList.size());//产生随机数 51 if (cList.get(id) == true) {//如果该词语未被听写过 52 System.out.println(aList.get(id)); 53 cList.set(id, false); 54 scanner.nextLine();//用户回车后方可进行下一个词语听写 55 System.out.println(bList.get(id)); 56 i++; 57 } 58 } 59 System.out.println("---------听写结束d=====( ̄▽ ̄*)b---------"); 60 61 aBufferedReader.close();//关闭流 62 bBufferedReader.close(); 63 aFileReader.close(); 64 bFileReader.close(); 65 } catch (FileNotFoundException e) {//没有try-catch会报错 66 e.printStackTrace(); 67 } catch (IOException e) { 68 e.printStackTrace(); 69 } 70 } 71 }