我必须: 创建一个模拟一副纸牌的数组。例如,“1_of_diamonds”代表 A 钻石,“2_of_diamonds”代表 2 颗钻石,最多“13_of_diamonds”,即 代表钻石之王。花色梅花、红心和黑桃以类似的形式表示 方式。所有这些元素应该位于一个数组中。该数组应该使用 计数器控制循环。将数组的内容输出到屏幕。 洗牌。
我有代码可以对其进行洗牌,但我不知道如何使用计数器控制循环填充数组。
//这是我的代码
import java.util.Random;
public class Cards{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
} //end main
public String[] shuffle(String[] deck) {
Random rnd = new Random();
for (int i = deck.length - 1; i >= 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
String a = deck[index];
deck[index] = deck[i];
deck[i] = a;
}
return deck;
}
}// end class
最佳答案
下面的填充方法可能会有所帮助。
public static String[] populate(){
String[] cards=new String[52];
String[] types={"hearts", "spades", "clubs", "diamonds"};
int current = 0;
for(String type:types)
for(int i = 1; i <= 13 ; i++)
cards[current++] = i + "_of_" + type;
return cards;
}
关于java - (Java)我不知道如何用所需的计数器控制循环填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55859141/