我目前正在製作一個小程序來教我一些新的Java,而我被卡住了。我正在製作的節目,你進入一個預先設定的位置,它會給你多少距離的jist。除了我想添加兩個+字位置,除了每當我輸入它時,它都會直接到else語句,而不是給jist。我的代碼如下。不讀兩個字輸入
import java.util.*;
public class Distance {
String _destination;
public Distance() {
Scanner scan = new Scanner(System.in);
System.out.println("Where do you want to go?");
_destination = scan.next();
if(getDistance() >= 2000) {
System.out.println("F*cking woop c*nt");
} else if(getDistance() >= 500) {
System.out.println("F*cking far c*nt");
} else if(getDistance() < 0) {
System.out.println("What are you? Retarded?");
} else if(getDistance().equals("0") {
System.out.println("Why bother?");
} else {
System.out.println("Walk it c*nt");
}
}
public int getDistance() {
int distance = 0;
if(_destination.equalsIgnoreCase("Ellenbrook")) {
distance = 1200;
} else if(_destination.equalsIgnoreCase("Midland")) {
distance = 900;
} else if(_destination.equalsIgnoreCase("Perth")) {
distance = 200;
} else if(_destination.equalsIgnoreCase("Woop Woop")) {
distance = 2100;
} else if(_destination.equalsIgnoreCase("High Wycombe")) {
distance = -2;
}
return distance;
}
public static void main(String[] args) {
new Distance();
}
}
你getDistance的()方法返回INT,但如果(getDistance的()等於(」你用別的。 0)))在你的構造函數 - 嘗試其他如果(getDistance()== 0)(當然,這不能解決你的掃描問題) – 2012-02-19 10:28:49
我做了其他if(String.valueOf(getDistance())。equals(「0 「){},因爲我一直得到一個derefenced int錯誤。 – 2012-02-19 10:31:42