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); } 

b是一个int

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

根据你的代码,它可能应该读取showcard1.setBackgroundResource(rank [b]);

尝试更改为int b = randInt.nextInt((rank.length)) - 1; (因为rank.length = 13,你的数组从0到12索引)