作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个游戏,可以根据顺序在屏幕上显示图案。我不希望这个顺序对 1 2 3 4 5 6
这样的顺序非常严格,我希望顺序更加随机。所以我试图创建一个方法来生成 1-6
的随机顺序。我尝试创建一种可以执行此操作的方法,但失败了。谁能帮我解决这个问题吗?
P.s.顺便说一句,这是在java中。
public static Random rand = new Random();
public static int[] array = new int[]{0,0,0,0,0,0};
public static void main(String[] args)
{
for(int i =0;i<6;i++)
{
for (int a =0;a<6;a++)
{
array[i] = rand.nextInt(6)+1;
while(array[i] == array[a])
{
array[i] = rand.nextInt(6)+1;
}
}
}
}
最佳答案
Collections.shuffle()
就在上面示例代码:
final List<Integer> list16 = Lists.newArrayList(1,2,3,4,5,6); // guava Lists
Collections.shuffle(list16);
// use list16
或不加 Guava :
final List<Integer> list16 = new ArrayList<Integer>();
for (int i = 1; i < 7; i++) {
list16.add(i);
}
Collections.shuffle(list16);
// use list16
关于java - 我怎样才能让这种随机方法发挥作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20755631/
在 Play 1.x 中有很棒的 play Idealize(和 play eclipsify),它为您最喜欢的 IDE 中的 Play 项目准备了项目文件。 我看到这是在 Play 2.X 中删除的
我是一名优秀的程序员,十分优秀!