我正在創建一個二十一點程序,並試圖在程序開始時向玩家發送隨機卡。這是我用Java編寫的函數,用於向玩家初始交易牌。Java在for循環中生成隨機數
public static int[][] initDeal(int NPlayers)
{
int hands[][] = new int[NPlayers][2];
for(int a = 0; a<NPlayers; a++)
{
hands[a][0] = (int)Math.round((Math.random() * 13))-1;
hands[a][1] = (int)Math.round((Math.random() * 13))-1;
}
return hands;
}
我認爲這是與隨機方法的問題,並在for循環中,雖然被隨機生成的兩個卡每個球員,所有球員都處理相同的牌。
你的問題是什麼? – Masudul
如果我是你,我會換出你的多維數組以獲得一個「Hand」對象的列表或數組。將使它更清潔。 – christopher
爲什麼不使用java.util.Random.nextInt(13)'? – Mureinik