当前位置:   article > 正文

JAVA默写单词小程序

JAVA默写单词小程序

编写一个记单词默写单词两个功能的小程序

 

  1. package com.lu.word;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import lombok.ToString;
  6. @Data
  7. @AllArgsConstructor
  8. @NoArgsConstructor
  9. @ToString
  10. public class A {
  11. private String k;
  12. private String v;
  13. }
  1. package com.lu.word;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.*;
  5. public class English {
  6. //单词计分
  7. private static int scoring = 0;
  8. public static void main(String[] args) {
  9. while (true) {
  10. System.out.println("1.开始背单词 2.默写单词中文意思 3.退出");
  11. Scanner scanner = new Scanner(System.in);
  12. int num = 0;
  13. while (true) {
  14. try {
  15. num = scanner.nextInt();
  16. } catch (Exception e) {
  17. System.out.println("输入错误,请重新输入");
  18. scanner.nextLine(); // 清除错误的输入
  19. continue;
  20. }
  21. break;
  22. }
  23. switch (num) {
  24. case 1:
  25. System.out.println("开始背单词");
  26. Properties properties = new Properties();
  27. try(FileReader fileReader = new FileReader("resource/work.properties");){
  28. properties.load(fileReader);
  29. properties.forEach((k,v)->{
  30. try {
  31. Thread.sleep(2000);
  32. System.out.println(k+" : "+v);
  33. } catch (InterruptedException e) {
  34. throw new RuntimeException(e);
  35. }
  36. });
  37. } catch (IOException e) {
  38. throw new RuntimeException(e);
  39. }
  40. break;
  41. case 2:
  42. Properties properties1 = new Properties();
  43. try (FileReader fileReader = new FileReader("resource/work.properties");) {
  44. properties1.load(fileReader);
  45. System.out.println("开始默写单词中文意思");
  46. HashMap<Integer, A> map = new HashMap<>();
  47. final int[] a = {1};
  48. properties1.forEach((k, v) -> map.put(a[0]++, new A((String) k, (String) v)));
  49. //判断生成的随机数是否在这个数组中
  50. int[] arr =new int[10];
  51. //随机数对象
  52. Random random = new Random();
  53. //生成一个1到map的长度之间的随机数
  54. for (int j = 0; j <10 && j< map.size() ; j++) {
  55. int i;
  56. //判断生成的随机数是否在这个数组中
  57. while (true){
  58. i = random.nextInt(1,map.size()+1);
  59. //判断生成的随机数是否在这个数组中
  60. if (!WordService.contains(arr, i)) {
  61. break;
  62. }
  63. }
  64. for (int k = 0; k < 3; k++) {
  65. System.out.println("请输入单词翻译");
  66. System.out.println(map.get(i).getK());
  67. String next = scanner.next();
  68. if (next.equals(map.get(i).getV())) {
  69. scoring += 10;
  70. System.out.println("正确");
  71. break;
  72. } else {
  73. System.out.println("错误");
  74. }
  75. }
  76. arr[j] = i;
  77. }
  78. System.out.println("你的得分是:"+scoring);
  79. } catch (IOException e) {
  80. throw new RuntimeException(e);
  81. }
  82. break;
  83. case 3:
  84. System.out.println("退出");
  85. System.exit(0);
  86. break;
  87. default:
  88. System.out.println("输入错误,请重新输入");
  89. break;
  90. }
  91. }
  92. }
  93. }
  1. package com.lu.word;
  2. public class WordService {
  3. /**
  4. * 判断数组中是否包含某个数字
  5. * @param arr 数组
  6. */
  7. public static boolean contains(int[] arr, int num) {
  8. for (int j : arr) {
  9. if (j == num) {
  10. return true;
  11. }
  12. }
  13. return false;
  14. }
  15. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/904726
推荐阅读
相关标签
  

闽ICP备14008679号