【发布时间】:2020-07-21 14:40:34
【问题描述】:
此方法旨在洗牌。当前方法声明具有“void”返回类型,并且在运行代码时出现错误“方法声明无效;需要返回类型”。可以改用什么返回类型?
public void shuffle()
{
int count = 0;
int index = 0;
for(int i; i = 100; i++)
{
count = (int)Math.Random() * 52;
index = (int)Math.Random() * 52;
DeckofCards[count] = DeckofCards[index];
DeckofCards[index] = DeckofCards[count];
}
}
【问题讨论】:
-
如何交换两个整数变量
a和b的值? -
我建议使用不同的算法 - 将
i-th 卡和具有随机索引的卡从i + 1交换为 100:random.nextInt(i + 1, 101)。您需要事先创建一个Random实例:Random random = new Random()。
标签: java methods return declaration