2012-12-07 22 views
1

下面是我的代碼梭哈是球員,但如果(Stud.equals(「北」))說它不能比較是還有另一種方法來比較它們?我在Netbeans使用JOptionPane.showOptionDialog遊戲,我不知道如何做if語句

String[] options = new String[] {"North", "South", "East", "West"}; 

JOptionPane.showOptionDialog(null, p + " " + Stud.distance(Comp) + " away" + 
    " Which way do you want to move?", "" + p + " is at " + Stud.getX() + ", " + Stud.getY() , 
    JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, 
    null, options, options[0]); 

if (options.equals("North")){ 
    Stud.moveNorth(); 
} 
else if (options.equals("South")){ 
    Stud.moveSouth(); 
} 
else if (options.equals("West")){ 
    Stud.moveWest(); 
} 
else if (options.equals("East")){ 
    Stud.moveSouth(); 
} 

回答

1

你需要int compareTo(String anotherString)

另外,JOptionPane的返回,你需要存儲的值。

像這樣

int optionChosen = JOptionPane.showOptionDialog(null, p + " " + Stud.distance(Comp) + " away" + 
    " Which way do you want to move?", "" + p + " is at " + Stud.getX() + ", " + Stud.getY() , 
    JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, 
    null, options, options[0]); 

if (options[optionChosen].compareTo("North") == 0){ 
    Stud.moveNorth(); 
} 

,或者你可以讓這個更簡單,使用通過上述的JOptionPane返回int做出方向的選擇。

像這樣:

if (optionChosen == 0){ 
    Stud.moveNorth(); 
} 
else if(optionChosen == 1){ 
    Stud.moveSouth(); 
} 
...and so on. 
相關問題