[关闭]
@Sarah 2015-11-05T17:52:25.000000Z 字数 1697 阅读 1674

JAVA 读取六级单词内容并加进数组

java笔记
上次课的作业更改后

  1. //读取六级单词内容并加进数组
  2. import java.io.*;
  3. import java.util.*;
  4. public class ReadWords {
  5. public static void main(String[] args) throws Exception {
  6. FileReader sf= new FileReader("六级词库副本.txt");
  7. BufferedReader br = new BufferedReader(sf);
  8. List<String> ewords=new ArrayList<String>();
  9. List<String> cwords=new ArrayList<String>();
  10. while(true){
  11. String s=br.readLine();
  12. if(s!=null){
  13. if(s.startsWith("[ "))
  14. cwords.add(br.readLine());
  15. else if(s.trim().length()!=0)
  16. ewords.add(s);
  17. }
  18. else
  19. break;
  20. }
  21. //System.out.println(ewords.get(0)+"\t"+cwords.get(0));
  22. BufferedReader kr=new BufferedReader(new InputStreamReader(System.in));
  23. while(true){
  24. int i=(int)(Math.random()*ewords.size());
  25. System.out.println(cwords.get(i));
  26. System.out.print(":");
  27. String s=kr.readLine();
  28. if(s.equals(ewords.get(i)))
  29. System.out.println("OK!");
  30. else
  31. System.out.println("error:"+ewords.get(i));
  32. System.out.print("continue?(y/n)");
  33. if(kr.readLine().equals("n"))
  34. break;
  35. }
  36. }
  37. public static void main2(String[] args) throws Exception {
  38. FileReader sf= new FileReader("六级.txt");
  39. BufferedReader br = new BufferedReader(sf);
  40. List<String> al=new ArrayList<String>();
  41. while(true){
  42. String s=br.readLine();
  43. if(s!=null)
  44. al.add(s);
  45. else {
  46. break;
  47. }
  48. }
  49. System.out.println(al.contains("adhere"));
  50. System.out.println(al.get(10));
  51. System.out.println(al.get(11));
  52. al.remove(10);
  53. System.out.println(al.get(10));
  54. al.clear();
  55. System.out.println(al.size());
  56. }
  57. //读取六级单词内容并加入数组里
  58. public static void main1(String[] args) {
  59. try {
  60. int i= 0;
  61. FileReader s = new FileReader("六级.txt");
  62. BufferedReader br = new BufferedReader(s);
  63. String[] words= new String[9000];
  64. while (true) {
  65. words[i] = br.readLine();
  66. if (words[i]==null)
  67. break;
  68. i++;
  69. }
  70. s.close();
  71. System.out.println(i);
  72. System.out.println(words[(int)(i*Math.random())]);
  73. } catch (Exception e) {
  74. // TODO Auto-generated catch block
  75. e.printStackTrace();
  76. }
  77. }
  78. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注