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

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

How to Shuffle char From Array of Strings?

问题

String names_ofcolor[] = {"red", "green", "blue"};

int random = (int) (Math.random() * names_ofcolor.length);

for (int j = 0; j < names_ofcolor[random].length(); j++) {

     Button btn = new Button(this);

    btn.setId(j);
    btn.setBackgroundColor(Color.WHITE);
    btn.setTextSize(16);
    linearlayout.addView(btn);
    btn.setText("" + names_ofcolor[random].charAt(j));

}
英文:

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

 String names_ofcolor[] = {&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;};

 int random = (int) (Math.random() * names_ofcolor.length);

    for (int j = 0; j &lt; names_ofcolor[random].length(); j++) {


         Button btn = new Button(this);

        btn.setId(j);
        btn.setBackgroundColor(Color.WHITE);
        btn.setTextSize(16);
        linearlayout.addView(btn);
        btn.setText(&quot;&quot; + names_ofcolor[random].charAt(j));

    }

答案1

得分: 0

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

String names_ofcolor[] = { "red", "green", "blue" };

int random = (int) (Math.random() * names_ofcolor.length);
List<Integer> indices = new ArrayList<Integer>();
for (int i = 0; i < names_ofcolor[random].length(); i++) {
    indices.add(i);
}
Collections.shuffle(indices);
for (int j = 0; j < names_ofcolor[random].length(); j++) {    
    Button btn = new Button(this);    
    btn.setId(j);
    btn.setBackgroundColor(Color.WHITE);
    btn.setTextSize(16);
    linearlayout.addView(btn);
    btn.setText("" + names_ofcolor[random].charAt(indices.get(j)));
}

一个快速演示:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String names_ofcolor[] = { "red", "green", "blue" };

        int random = (int) (Math.random() * names_ofcolor.length);
        List<Integer> indices = new ArrayList<Integer>();
        for (int i = 0; i < names_ofcolor[random].length(); i++) {
            indices.add(i);
        }
        Collections.shuffle(indices);
        for (int j = 0; j < names_ofcolor[random].length(); j++) {
            String label = "" + names_ofcolor[random].charAt(indices.get(j));
            System.out.print(label);
        }
    }
}

一个样本运行:

regne

另一个样本运行:

erd

另一个样本运行:

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.

String names_ofcolor[] = { &quot;red&quot;, &quot;green&quot;, &quot;blue&quot; };

int random = (int) (Math.random() * names_ofcolor.length);
List&lt;Integer&gt; indices = new ArrayList&lt;Integer&gt;();
for (int i = 0; i &lt; names_ofcolor[random].length(); i++) {
	indices.add(i);
}
Collections.shuffle(indices);
for (int j = 0; j &lt; names_ofcolor[random].length(); j++) {    
	Button btn = new Button(this);    
	btn.setId(j);
	btn.setBackgroundColor(Color.WHITE);
	btn.setTextSize(16);
	linearlayout.addView(btn);
	btn.setText(&quot;&quot; + names_ofcolor[random].charAt(indices.get(j)));
}

A quick demo:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
	public static void main(String[] args) {
		String names_ofcolor[] = { &quot;red&quot;, &quot;green&quot;, &quot;blue&quot; };

		int random = (int) (Math.random() * names_ofcolor.length);
		List&lt;Integer&gt; indices = new ArrayList&lt;Integer&gt;();
		for (int i = 0; i &lt; names_ofcolor[random].length(); i++) {
			indices.add(i);
		}
		Collections.shuffle(indices);
		for (int j = 0; j &lt; names_ofcolor[random].length(); j++) {
			String label = &quot;&quot; + names_ofcolor[random].charAt(indices.get(j));
			System.out.print(label);
		}
	}
}

A sample run:

regne

Another sample run:

erd

Another sample run:

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:

确定