首页 > 解决方案 > 如何在 Java 中声明一个洗牌的方法?

问题描述

此方法旨在洗牌。当前方法声明具有“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];
    }
    
}

标签: javamethodsreturndeclaration

解决方案


问题是for循环。您声明inti但从不初始化。条件段当前是一个分配。您需要初始化并更改条件。

for (int i = 0; i < 100; i++) {

}

推荐阅读