混合一个字母数组

huangapple go评论104阅读模式
英文:

Mixing up an array of letters

问题

以下是翻译好的内容:

  1. 对于我的作业我们得到了一个程序该程序使用字母表的前四个字母创建一个4x4的数组数组已经被排序好了除了作业中的混淆部分我已经完成了所有的内容混淆函数是从构造函数中调用的而不是从主方法中调用的它的作用是打乱数组中的字母顺序到目前为止我尝试过使用集合的shuffle方法但我发现它用于整数而不是字符值我还尝试将数组转换为列表但我无法弄清楚如何将列表放回数组中我应该如何打乱数组中的字母顺序
  2. public LetterPuzzle() {
  3. puzzle = new char[4][4];
  4. letters = new char[4];
  5. for (int x=0; x<letters.length; x++) {
  6. letters = makeRow();
  7. puzzle[x] = letters;
  8. }
  9. System.out.println("Original: " + puzzle);
  10. scramble();
  11. }
  12. private char[] makeRow()
  13. {
  14. char [] let = new char[4];
  15. for (char c='A'; c<'E'; c++)
  16. let[(int)(c-'A')] = c;
  17. return let;
  18. }
  19. /**
  20. * 将谜题表示为带有行和列标题的4x4矩阵
  21. * @return 以矩阵形式表示的谜题字符串
  22. */
  23. public String toString() {
  24. String s = " 0 1 2 3\n";
  25. s = s + "--------------\n";
  26. for (int x=0; x<puzzle.length; x++) {
  27. s = s + x + "|";
  28. for (int y=0; y<puzzle.length; y++) {
  29. s = s + " " + puzzle[x][y]+ " ";
  30. }
  31. s = s + "\n";
  32. }
  33. return s;
  34. }
  35. /******由学生完成*****/
  36. /**
  37. * 检查一行中是否有重复项
  38. * @param row 要检查的行:必须是值在0到puzzle.length之间
  39. * @return 如果有重复项返回false,否则返回true
  40. * @throws IllegalArgumentException 如果行超出范围
  41. */
  42. public boolean checkRow(int row) {
  43. for (row = 0; row < puzzle.length; row++){
  44. for(int j = row + 1 ; j < puzzle.length; j++) {
  45. if( puzzle[row].equals(puzzle[j])) {
  46. return false;
  47. }
  48. else if(row > 0) {
  49. throw new IllegalArgumentException("行超出范围");
  50. }
  51. return true;
  52. }
  53. }
  54. return true;
  55. }
  56. /**
  57. * 检查一列中是否有重复项
  58. * @param col 要检查的列:必须是值在0到puzzle.length之间
  59. * @return 如果有重复项返回false,否则返回true
  60. * @throws IllegalArgumentException 如果列超出范围
  61. */
  62. public boolean checkColumn(int col) {
  63. for(col = 0; col < puzzle.length; col++){
  64. for(int j = col + 1; j < puzzle.length; j++) {
  65. if( puzzle[col].equals(puzzle[j])) {
  66. return false;
  67. }
  68. else if(col > 0) {
  69. throw new IllegalArgumentException("列超出范围");
  70. }
  71. }
  72. }
  73. return true;
  74. }
  75. /**
  76. * 打乱谜题,使字母随机排序
  77. */
  78. void scramble( ){
  79. }
  80. public static void main (String [] args) {
  81. LetterPuzzle puzz = new LetterPuzzle();
  82. System.out.println(puzz);
  83. // 在此处添加代码来测试您的方法并显示结果
  84. }
英文:

For my assignment we were given a program that creates a 4x4 array using the first four letters of the alphabet. The array already is sorted to start out. I have completed everything except for the scramble part of the assignment. Scramble is called from the constructor, not the main method and is supposed to mix up the letters within the array. So far I have tried to shuffle from collection but I have found that it is used for integers, not char values. I have also tried to convert from an array to a list but I can't figure out how to put the list back into the array. How can I mix up the letters within the array?

  1. public LetterPuzzle() {
  2. puzzle = new char[4][4];
  3. letters = new char[4];
  4. for (int x=0; x&lt;letters.length; x++) {
  5. letters = makeRow();
  6. puzzle[x] = letters;
  7. }
  8. System.out.println(&quot;Original: &quot; + puzzle);
  9. scramble();
  10. }
  11. private char[] makeRow()
  12. {
  13. char [] let = new char[4];
  14. for (char c=&#39;A&#39;; c&lt;&#39;E&#39;; c++)
  15. let[(int)(c-&#39;A&#39;)] = c;
  16. return let;
  17. }
  18. /**
  19. * Represents puzzle as 4x4 matrix with row &amp; column headings
  20. * @return puzzle as matrix String
  21. */
  22. public String toString() {
  23. String s = &quot; 0 1 2 3\n&quot;;
  24. s = s + &quot;--------------\n&quot;;
  25. for (int x=0; x&lt;puzzle.length; x++) {
  26. s = s + x + &quot;|&quot;;
  27. for (int y=0; y&lt;puzzle.length; y++) {
  28. s = s + &quot; &quot; + puzzle[x][y]+ &quot; &quot;;
  29. }
  30. s = s + &quot;\n&quot;;
  31. }
  32. return s;
  33. }
  34. /******TO BE COMPLETED BY STUDENT*****/
  35. /**
  36. * Checks for duplicates in a row
  37. * @param row is the row to be checked: must be a value 0 .. puzzle.length
  38. * @return false if there are duplicates, true if not
  39. * @throws IllegalArgumentException if row is out of bounds
  40. */
  41. public boolean checkRow(int row) {
  42. for (row = 0; row &lt; puzzle.length; row++){
  43. for(int j = row + 1 ; j &lt; puzzle.length; j++) {
  44. if( puzzle[row].equals(puzzle[j])) {
  45. return false;
  46. }
  47. else if(row &gt; 0) {
  48. throw new IllegalArgumentException(&quot;Row is out of bounds&quot;);
  49. }
  50. return true;
  51. }
  52. }
  53. return true;
  54. }
  55. /**
  56. * Checks for duplicates in a column
  57. * @param col is the column to be checked: must be a value 0 .. puzzle.length
  58. * @return false if there are duplicates, true if not
  59. * @throws IllegalArgumentException if row is out of bounds
  60. */
  61. public boolean checkColumn(int col) {
  62. for(col = 0; col &lt; puzzle.length; col++){
  63. for(int j = col + 1; j &lt; puzzle.length; j++) {
  64. if( puzzle[col].equals(puzzle[j])) {
  65. return false;
  66. }
  67. else if(col &gt; 0) {
  68. throw new IllegalArgumentException(&quot;Column out of bounds&quot;);
  69. }
  70. }
  71. }
  72. return true;
  73. }
  74. /**
  75. * Scrambles puzzle so that letters appear in random order
  76. */
  77. void scramble( ){
  78. }
  79. public static void main (String [] args) {
  80. LetterPuzzle puzz = new LetterPuzzle();
  81. System.out.println(puzz);
  82. // Add code here to test your methods &amp; display results
  83. }

答案1

得分: 2

如果您使用一个Character包装类来存储字符,您可以轻松地进行如下洗牌操作:

  1. Character[] chars =
  2. { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
  3. Collections.shuffle(Arrays.asList(chars));
  4. System.out.println(Arrays.toString(chars));

打印结果:

  1. [g, b, d, a, c, i, f, e, h]

如果您想要使用原始数组,您可以这样进行洗牌操作:

  1. char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
  • 生成一个介于0arrayLength-1(包括)之间的索引next
  • 交换chars[next]chars[i]
  • chars[next]现在已经固定。
  • 重复这个过程,生成一个介于0arrayLength-2之间的索引。
  • i == 0时,操作完成。
  1. public static void shuffle(char[] chars) {
  2. Random r = new Random();
  3. for (int i = chars.length-1; i >= 0; i--) {
  4. int next = r.nextInt(i+1);
  5. char c = chars[next];
  6. chars[next] = chars[i];
  7. chars[i] = c;
  8. }
  9. }
英文:

If you use a Character wrapper class to store your characters you can easily shuffle it as follows:

  1. Character[] chars =
  2. { &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39; };
  3. Collections.shuffle(Arrays.asList(chars));
  4. System.out.println(Arrays.toString(chars));

Prints

  1. [g, b, d, a, c, i, f, e, h]

If you want to use primitive arrays you can shuffle them like this.

  1. char[] chars = { &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;};
  • generate an index next between 0 and the arrayLength-1 inclusive
  • swap chars[next] with chars[i].
  • chars[next] is now permanently fixed.
  • repeat the process, generating an index between 0 and arrayLength-2.
  • when i == 0, you're done.
  1. public static void shuffle(char[] chars) {
  2. Random r = new Random();
  3. for (int i = chars.length-1; i &gt;= 0; i--) {
  4. int next = r.nextInt(i+1);
  5. char c = chars[next];
  6. chars[next] = chars[i];
  7. chars[i] = c;
  8. }
  9. }
  10. </details>
  11. # 答案2
  12. **得分**: 0

尝试为每个新数组索引获取随机位置。类似这样的内容。

  1. void scramble () {
  2. puzzle = new char[4][4];
  3. scrambledPuzzle = new char[4][4]; // 将所有位置填充为空字符或其他内容
  4. letters = new char[4];
  5. int letterIndex = 0;
  6. for (int x=0; x<letters.length; x++) {
  7. for (int y=0; y<letters.length; y++) {
  8. while (true) {
  9. Random rand = new Random();
  10. int upperbound = letters.length;
  11. int index = rand.nextInt(upperbound); // 获取当前字母的随机位置
  12. if(scrambledPuzzle[x][index] == ''){ // 尚未填充的位置
  13. scrambledPuzzle[x][index] = letters[y]; // 将字母放置在这里
  14. break;
  15. }
  16. }
  17. }
  18. }
  19. }
英文:

try to get a random position for each new array index. something like this.

  1. void scramble () {
  2. puzzle = new char[4][4];
  3. scrambledPuzzle = new char[4][4]; // fill all of this with empty char or
  4. something
  5. letters = new char[4];
  6. int letterIndex = 0;
  7. for (int x=0; x&lt;letters.length; x++) {
  8. for (int y=0; y&lt;letters.length; y++) {
  9. for( true ) {
  10. Random rand = new Random();
  11. int upperbound = letter.length;
  12. int index = rand.nextInt(upperbound) ; //get a random position for current letter
  13. if(scrambledPuzzle[x][index] == &#39;&#39;){ // not yet filled position
  14. scrambledPuzzle[x][index] = letters[y]; // place it here
  15. break;
  16. }
  17. }
  18. }
  19. }
  20. }

huangapple
  • 本文由 发表于 2020年9月22日 01:37:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63997366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定