Android随机数

时间:2010-07-23 06:14:38

标签: java android

我从数组生成一张随机卡。并分配它。' 下面是代码..但它显示错误。 有什么问题?

public void rand() {
    String rank[]=  {"tclub1.png", "tclub2.png", "tclub3.png", "tclub4.png", "tclub5.png", "tclub6.png", "tclub7.png", "tclub8.png", "tclub9.png", "tclub10.png","tclub11.png", "tclub12.png", "tclub13.png"};

    Random randInt = new Random();

    int b = randInt.nextInt((rank.length));
    showcard1.setBackgroundResource(b); 
}

2 个答案:

答案 0 :(得分:4)

b是一个int

因此您需要在代码中的某个位置对[b]数组进行排名

根据你的代码,也许应该阅读 showcard1.setBackgroundResource(秩并[b]);

答案 1 :(得分:3)

尝试更改为int b = randInt.nextInt((rank.length)) - 1;(因为rank.length = 13且您的数组的编号从0到12)