如何从字符串数组中洗牌字符?

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

How to Shuffle char From Array of Strings?

问题

  1. String names_ofcolor[] = {"red", "green", "blue"};
  2. int random = (int) (Math.random() * names_ofcolor.length);
  3. for (int j = 0; j < names_ofcolor[random].length(); j++) {
  4. Button btn = new Button(this);
  5. btn.setId(j);
  6. btn.setBackgroundColor(Color.WHITE);
  7. btn.setTextSize(16);
  8. linearlayout.addView(btn);
  9. btn.setText("" + names_ofcolor[random].charAt(j));
  10. }
英文:

> i am trying to make game guess the word but I cant shuffle char from
> array of string

  1. String names_ofcolor[] = {&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;};
  2. int random = (int) (Math.random() * names_ofcolor.length);
  3. for (int j = 0; j &lt; names_ofcolor[random].length(); j++) {
  4. Button btn = new Button(this);
  5. btn.setId(j);
  6. btn.setBackgroundColor(Color.WHITE);
  7. btn.setTextSize(16);
  8. linearlayout.addView(btn);
  9. btn.setText(&quot;&quot; + names_ofcolor[random].charAt(j));
  10. }

答案1

得分: 0

创建一个从 0names_ofcolor[random].length() - 1 的索引列表,并使用 Collections::shuffle 进行洗牌。然后,您可以使用洗牌后的索引来设置按钮的标签。

  1. String names_ofcolor[] = { "red", "green", "blue" };
  2. int random = (int) (Math.random() * names_ofcolor.length);
  3. List<Integer> indices = new ArrayList<Integer>();
  4. for (int i = 0; i < names_ofcolor[random].length(); i++) {
  5. indices.add(i);
  6. }
  7. Collections.shuffle(indices);
  8. for (int j = 0; j < names_ofcolor[random].length(); j++) {
  9. Button btn = new Button(this);
  10. btn.setId(j);
  11. btn.setBackgroundColor(Color.WHITE);
  12. btn.setTextSize(16);
  13. linearlayout.addView(btn);
  14. btn.setText("" + names_ofcolor[random].charAt(indices.get(j)));
  15. }

一个快速演示:

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. public class Main {
  5. public static void main(String[] args) {
  6. String names_ofcolor[] = { "red", "green", "blue" };
  7. int random = (int) (Math.random() * names_ofcolor.length);
  8. List<Integer> indices = new ArrayList<Integer>();
  9. for (int i = 0; i < names_ofcolor[random].length(); i++) {
  10. indices.add(i);
  11. }
  12. Collections.shuffle(indices);
  13. for (int j = 0; j < names_ofcolor[random].length(); j++) {
  14. String label = "" + names_ofcolor[random].charAt(indices.get(j));
  15. System.out.print(label);
  16. }
  17. }
  18. }

一个样本运行:

  1. regne

另一个样本运行:

  1. erd

另一个样本运行:

  1. elbu
英文:

Create a List of indices from 0 to names_ofcolor[random].length() - 1 and shuffle it using Collections::shuffle. Then, you can use the shuffled indices to set the label of the button.

  1. String names_ofcolor[] = { &quot;red&quot;, &quot;green&quot;, &quot;blue&quot; };
  2. int random = (int) (Math.random() * names_ofcolor.length);
  3. List&lt;Integer&gt; indices = new ArrayList&lt;Integer&gt;();
  4. for (int i = 0; i &lt; names_ofcolor[random].length(); i++) {
  5. indices.add(i);
  6. }
  7. Collections.shuffle(indices);
  8. for (int j = 0; j &lt; names_ofcolor[random].length(); j++) {
  9. Button btn = new Button(this);
  10. btn.setId(j);
  11. btn.setBackgroundColor(Color.WHITE);
  12. btn.setTextSize(16);
  13. linearlayout.addView(btn);
  14. btn.setText(&quot;&quot; + names_ofcolor[random].charAt(indices.get(j)));
  15. }

A quick demo:

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. public class Main {
  5. public static void main(String[] args) {
  6. String names_ofcolor[] = { &quot;red&quot;, &quot;green&quot;, &quot;blue&quot; };
  7. int random = (int) (Math.random() * names_ofcolor.length);
  8. List&lt;Integer&gt; indices = new ArrayList&lt;Integer&gt;();
  9. for (int i = 0; i &lt; names_ofcolor[random].length(); i++) {
  10. indices.add(i);
  11. }
  12. Collections.shuffle(indices);
  13. for (int j = 0; j &lt; names_ofcolor[random].length(); j++) {
  14. String label = &quot;&quot; + names_ofcolor[random].charAt(indices.get(j));
  15. System.out.print(label);
  16. }
  17. }
  18. }

A sample run:

  1. regne

Another sample run:

  1. erd

Another sample run:

  1. elbu

huangapple
  • 本文由 发表于 2020年5月5日 19:55:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61612552.html
匿名

发表评论

匿名网友

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

确定