英文:
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[] = {"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));
}
答案1
得分: 0
创建一个从 0
到 names_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[] = { "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)));
}
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[] = { "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);
}
}
}
A sample run:
regne
Another sample run:
erd
Another sample run:
elbu
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论