我正在嘗試創建一副擁有81張獨特卡牌的紙牌(基於SET遊戲)。我試圖通過使用三個廣義for循環來實現這一點。現在,我有三個具有特徵(形狀,顏色,圖案)的字符串數組。但是,在將println
語句添加到循環中後,我發現只有一種類型的模式纔會創建紅卡。如何使用嵌套for/each循環來創建具有不同特徵的對象?
ArrayList<Card> deck = new ArrayList<Card>();
static public Deck create() {
Deck deck = new Deck();
String[] shapes = new String[] { "diamond", "squiggly", "oval",
"double diamond", "double squiggly", "double oval",
"triple diamond", "triple squiggly", "triple oval" };
String[] colors = new String[] { "red", "green", "purple" };
String[] patterns = new String[] { "stripes", "filled in", "outlined" };
// Shapes
for (String shape : shapes) {
System.out.println(shape);
// Colors
for (String color : colors) {
System.out.println(color);
// Patterns
for (String pattern : patterns) {
System.out.println(pattern + "\n");
Card card = new Card(shape, color, pattern);
deck.deck.add(card);
break;
}
break;
}
}
return null;
}
這是輸出:
diamond
red
stripes
squiggly
red
stripes
oval
red
stripes
double diamond
red
stripes
double squiggly
red
stripes
double oval
red
stripes
triple diamond
red
stripes
triple squiggly
red
stripes
triple oval
red
stripes
現在,它的目的是爲了建立所有每一個人的圖案和形狀的形狀。我完全失去了這一點,我會很感激任何幫助!
這裏引起混淆的原因可能是您每次形狀迭代只打印一次形狀。如果你將'System.out.println'語句移動到最內層的循環中(就像我在ideone例子中所做的那樣),這會更有意義。 – aioobe 2014-09-29 18:59:43