如何在java中防止从数组中重复选择

wz8daaqr  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(360)

我正在用jgrasp制作一个程序,将文本文件中的单词存储在数组中。有2个文件和2个数组,一个存储拼写正确的单词,另一个存储拼写错误的单词。然后程序从两个数组中随机选择一个单词,并询问用户拼写是否正确。我已经让一切运行良好,但需要一种方法,以防止单词被选中不止一次。代码如下:

  1. import java.util.*;
  2. import java.io.*;
  3. public class wordGame
  4. {
  5. public static void main(String[] args) throws Exception
  6. {
  7. boolean more = true;
  8. Scanner kb = new Scanner(System.in);
  9. while(more)
  10. {
  11. run();
  12. System.out.println("Another player?");
  13. String answer = kb.next();
  14. if(answer.equalsIgnoreCase("no"))
  15. more = false;
  16. }
  17. System.out.println("Good Bye");
  18. }
  19. public static void run() throws Exception
  20. {
  21. String[] correct = new String[10];
  22. String[] incorrect = new String[10];
  23. //static void description()
  24. description();
  25. //static void fillArray(String[] correct, String[] incorrect)
  26. fillArray(correct, incorrect);
  27. //static void play(String[] correct, String[] incorrect)
  28. play(correct, incorrect);
  29. }
  30. //actual play
  31. public static void play(String[] correct, String[] incorrect)
  32. {
  33. Scanner kb = new Scanner(System.in);
  34. Random rand = new Random();
  35. int points = 0;
  36. for(int i = 1; i <= 10; i++)
  37. {
  38. //genreate a random number either 0 or 1
  39. int r = rand.nextInt(2);
  40. if(r == 0)
  41. {
  42. //generate a random number between 0-9 inclusive (rand.nextInt(10))
  43. int index = rand.nextInt(10);
  44. //display the word on the string from the correct array at the given index
  45. System.out.println("Is the word***"+correct[index]+"***spelled correctly?");
  46. //ask the the user to enter if the word is spelled correctly or not y/n
  47. System.out.print("y/n: ");
  48. String answer = kb.next();
  49. //if the users answer is yes increment points by 1, otherwise decrement by 1
  50. if(answer.equalsIgnoreCase("y"))
  51. points++;
  52. else
  53. points--;
  54. }
  55. else
  56. {
  57. int index = rand.nextInt(10);
  58. //display the word on the string from the correct array at the given index
  59. System.out.println("Is the word***"+incorrect[index]+"***spelled correctly?");
  60. //ask the the user to enter if the word is spelled correctly or not y/n
  61. System.out.print("y/n: ");
  62. String answer = kb.next();
  63. //if the users answer is yes increment points by 1, otherwise decrement by 1
  64. if(answer.equalsIgnoreCase("n"))
  65. points++;
  66. else
  67. points--;
  68. }
  69. }
  70. System.out.println("Your score: "+points);
  71. }
  72. public static void fillArray(String[] correct, String[] incorrect) throws Exception
  73. {
  74. //create a file object
  75. File f1 = new File("correct.txt");
  76. //create a pointer to the file
  77. Scanner input1 = new Scanner(f1);//input: name of scanner pulling info from file
  78. int index1 = 0;
  79. //fill in the array correct
  80. while(input1.hasNextLine() && index1 < correct.length)
  81. {
  82. //read one word
  83. String word = input1.nextLine();
  84. //store it in the array
  85. correct[index1] = word;
  86. index1++;
  87. }
  88. //create a file object
  89. File f2 = new File("incorrect.txt");
  90. //create a pointer to the file
  91. Scanner input2 = new Scanner(f2);//input: name of scanner pulling info from file
  92. int index2 = 0;
  93. //fill in the array incorrect
  94. while(input2.hasNextLine() && index2 < incorrect.length)
  95. {
  96. //read one word
  97. String word = input2.nextLine();
  98. //store it in the array
  99. incorrect[index2] = word;
  100. index2++;
  101. }
  102. }
  103. // displays instructions for the game
  104. public static void description()
  105. {
  106. System.out.printf("%40s", "Welcome to Misspelled!!");
  107. System.out.printf("\n%41s", "Please play resonbsibly!!");
  108. System.out.println("");
  109. System.out.printf("\n%39s", "THE RULES OF THE GAME\n");
  110. System.out.println("You will be shown a series of 10 words that are spelled correctly or incorrectly");
  111. System.out.println("Answer y if it is spelled correctly, n if it is spelled incorrectly");
  112. System.out.println("Correct answers will award you 1 point, incorrect answers will lose you 1 point");
  113. System.out.println("");
  114. }
  115. }
voase2hg

voase2hg1#

这里有一个方法。我用 ints 用于演示目的,但该方法很容易适应任何数据类型。这与@scarywombat的建议类似。它在运行中进行洗牌,但仍然使用随机选择。
它只是选择一个随机值 t .
移动索引为的值 size-1 进入 t's 狭槽
更新 size 返回 t 如果没有更多值,则返回-1

  1. int[] vals = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  2. int size = vals.length;
  3. Random r = new Random();
  4. int i;
  5. while ((i = getValue()) > 0) {
  6. System.out.print(i + " ");
  7. }

打印出类似于

  1. 3 1 8 9 10 2 6 7 4 5

方法
请注意,此方法不会保留列表的内容。如果要这样做,请取消注解注解掉的语句。但这份名单最终还是会随机化。

  1. public static int getValue() {
  2. if (size > 0) {
  3. int idx = r.nextInt(size);
  4. int t = vals[idx];
  5. vals[idx] = vals[--size];
  6. // vals[size] = t;
  7. return t;
  8. }
  9. return -1;
  10. }
展开查看全部

相关问题