测试颜色是否相等 [英] Testing if colors equal
问题描述
我正在从斯坦福演讲在iTunes U(仍然很漂亮的绿色)的突围作业,并陷入了一个咆哮。我试图设置一个点值为不同的彩色砖,所以我可以计算一个分数,但我的if的似乎不工作。我有一种感觉,getColor()不返回值,我认为是;我创建了一个状态标签来显示它的返回,但我仍然不能弄清楚如何测试。
以下是我正在处理的一段代码:
if(collider!= null&& collider!= paddle){
remove(scoreLabel);
vy = -vy;
color brickColor = collider.getColor();
add(new GLabel(+ collider.getColor(),10,12));
double temp = brickVal(brickColor)* scoreMultiplier;
score + = Math.abs(temp);
addScoreboard();
remove(collider);
}
}
private double brickVal(Color c){
if(c.equals(Color.RED)){
return 10.0;
} else if(c == Color.ORANGE){
return brickVal = 8.0;
} else if(c == Color.YELLOW){
return brickVal = 6.0;
} else if(c == Color.GREEN){
return brickVal = 4.0;
} else if(Color.CYAN.equals(c)){
return brickVal = 2.0;
} else if(c == Color.MAGENTA){
return brickVal = 1.0;
} else {
return 1.0;
}
}
如果你需要完整的代码让我知道。 / p>
使用 Color.X.equals(c)
就像 c == Color.X
。
您也可以使用 I'm working on the Breakout assignment from the Stanford lectures on iTunes U (still pretty green) and ran into a snarl. I'm trying to set a point value for the different colored bricks so I can calculate a score but my if's don't seem to work. I have a feeling that getColor() isn't returning the value that I think it is; I created a status label to show my what it's returning but I still can't figure out how to test for that. More than likely it's something simple I'm missing or just don't know of yet. Here's a snippet of the bit I'm working on: If you need the full code let me know. Use You could also use 这篇关于测试颜色是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! c.equals(Color.X)
,就像你为 Color.RED
所做的那样,但是很多人喜欢用另一种方法来保护<对于 c
为 null
的情况,if (collider != null && collider != paddle) {
remove(scoreLabel);
vy = -vy;
Color brickColor = collider.getColor();
add(new GLabel("" + collider.getColor(), 10, 12));
double temp = brickVal(brickColor) * scoreMultiplier;
score += Math.abs(temp);
addScoreboard();
remove(collider);
}
}
private double brickVal(Color c) {
if (c.equals(Color.RED)) {
return 10.0;
} else if (c == Color.ORANGE) {
return brickVal = 8.0;
} else if (c == Color.YELLOW) {
return brickVal = 6.0;
} else if (c == Color.GREEN) {
return brickVal = 4.0;
} else if (Color.CYAN.equals(c)) {
return brickVal = 2.0;
} else if (c == Color.MAGENTA) {
return brickVal = 1.0;
} else {
return 1.0;
}
}
Color.X.equals(c)
for your if cases that are like c == Color.X
. You're testing if the objects are the same instance, instead of if they're considered to be equal to each other.c.equals(Color.X)
like you did for Color.RED
, however many people prefer the other way to safeguard against a NullPointerException
for cases where c
is null
.