如果。。。其他的对于所有条件,if执行最终else
我是Java新手,正在尝试使用if。。。其他的if语句。看起来很简单,但是,不管其他条件是否为真,最终的“else”语句(默认值)都会执行。换句话说,对于所有条件,代码都返回0
这是我的代码:
public class Choice {
private int type; //stores the choice type: 0=rock, 1=paper, 2=scissors
private ColorImage choiceImage; //stores the image to be displayed on the canvas
public Choice(int type) {
//initialize the "type" instance varialble
this.type = type;
}
/**
* Get a number that represents the choice type
*
* @return a number that represents the choice type: 0=rock, 1=paper, 2=scissors
*/
public int getType() {
return type;
}
/**
* Compare "this" with anotherChoice
*
* @param anotherChoice the choice to be compared
* @return either 1, -1, or 0 which indicates the comparison result: 1 means "this" wins anotherChoice; -1 means "this" loses to anotherChoice; 0 means "this" and anotherChoice are the same
*/
public int compareWith(Choice anotherChoice) {
if ((this.type == 0) && (type == 1))
return -1;
else if (this.type == 0 && type == 2)
return 1;
else if (this.type == 1 && type == 0)
return 1;
else if (this.type == 1 && type == 2)
return -1;
else if (this.type == 2 && type == 0)
return -1;
else if (this.type == 2 && type == 1)
return 1;
else
return 0;
}
}
共 (0) 个答案