您好我是新來的Java和我需要一點點主動幫助的問題是從編碼蝙蝠:CodingBat邏輯-1> caughtSpeeding
您駕駛的是有點太快了,和警察阻止你。編寫代碼來計算結果,編碼爲一個int值:0 =無票,1 =小票,2 =大票。如果速度爲60或更低,則結果爲0.如果速度在61和80之間,結果爲1.如果速度爲81或更高,結果爲2.除非是您的生日 - 在當天,您的在所有情況下速度可以高5。
public int caughtSpeeding(int speed, boolean isBirthday) {
Integer int2 = 0;
if (speed <= 60){
int2 = 0;
}
if (isBirthday = true){
if (speed >=61 && speed <= 85){
int2 = 1;
}
if (speed >= 86){
int2 = 2;
}
}
if (isBirthday = false){
if (speed >=61 && speed <=80){
int2 = 1;
}
if (speed >= 81){
int2 = 2;
}
}
return int2;
}
我越來越caughtSpeeding(65,真正的)應該是0的時候我的代碼運行到= 1和caughtSpeeding(85,假的)應爲2時,我的代碼運行爲= 1一次。
由於
'='分配,''==平等 – Guy
你說你是一個初學者,所以在注意:沒有理由爲'int2'使用'Integer'類型而不是'int'。如果你可以選擇,在你自己保存自動裝箱時使用原始類型。看[int vs整數](https://stackoverflow.com/questions/8660691/what-is-the-difference-between-integer-and-int-in-java/8660812#8660812) – Rhayene