2016-08-14 51 views
-2

標題有點令人困惑,但我想向整個顏色方法添加一個整數。我的意思是,如果我有三種顏色,並希望通過他們去一個for循環(類似下面那個):使用整數獲取顏色

Color color1 = something; 
Color color2 = something; 
Color color3 = something; 

for (int i = 1; i < 4; i++) { 
    int r = color(i).getRed(); 
    int g = color(i).getGreen(); 
    int b = color(i).getBlue(); 
} 
+0

我不明白你的問題......但你的循環看起來很奇怪。它在第一次迭代中不會做任何事情,因爲1> 4是錯誤的。 – Steve

+0

@解決我的錯誤。我希望在循環的第一次執行中使用第一種顏色(color1),在第二次循環中使用第二種顏色(color2)等。 – Eric

回答

0

您可以將三種顏色存儲在數組中,並訪問您的循環內部數組。

// take these three colors for example 
Color[] colors = {Color.BLACK, Color.WHITE, Color.YELLOW}; 

for (int i=0; i<3; i++) { 
    int r = colors[i].getRed(); 
    int g = colors[i].getGreen(); 
    int b = colors[i].getBlue(); 
} 
+0

爲什麼要循環遍歷循環3次? – discipline