if语句正在执行,else-if语句不在java中

时间:2014-03-17 15:20:24

标签: if-statement drop-down-menu

    import javax.swing.JOptionPane;
    public class sam{
    public static void main(String[]args){

    String a;
    String b;
    int c;

    sam s1 = new sam();

    a=s1.getInfo();
    c=s1.getBalance();
    b=s1.getMenu(a,c);

    }

//菜单方法从这里开始

    public String getMenu (String c, Integer d) {
String[] a;
String[] choices = { "Account Balance", "Deposit", "Withdraw", "User Account", "Exit options"};
String input = (String) JOptionPane.showInputDialog(null, "What would you like to do?",
    "ATM menu", JOptionPane.QUESTION_MESSAGE, null,choices,choices[0]);

if  ((choices[0] == choices[0])){
    JOptionPane.showMessageDialog(null,"Your Account Balance is: "+d,"ATM machine",JOptionPane.INFORMATION_MESSAGE);}

// if语句正确执行

 else if    ((choices[1] == choices[1])){
    String in=JOptionPane.showInputDialog("Deposit: ");
    int deposit=Integer.parseInt(in);
    int add=d+deposit;
    JOptionPane.showMessageDialog(null,"Your Current Balance: "+add,"ATM machine",JOptionPane.INFORMATION_MESSAGE);}

//但是当我选择帐户余额时,它会显示if语句而不是else - 如果一个

 else if    ((choices[2] == choices[2])){
    String in=JOptionPane.showInputDialog("Withdraw: ");
    int withdraw=Integer.parseInt(in);
    int sub=d+withdraw;
    JOptionPane.showMessageDialog(null,"Your Current Balance: "+sub,"ATM machine",JOptionPane.INFORMATION_MESSAGE);}

 else if    ((choices[3] == choices[3])){
    JOptionPane.showMessageDialog(null," "+c,"ATM machine",JOptionPane.INFORMATION_MESSAGE);}

 else if    ((choices[4] == choices[4])){
    JOptionPane.showMessageDialog(null,"The program will be terminated in a few seconds","ATM machine",JOptionPane.INFORMATION_MESSAGE);
 }
 return input;
  }

//我对编程很陌生,我赶紧把它编入总决赛。

1 个答案:

答案 0 :(得分:0)

您的所有if语句都将评估为true。我认为您的意图是将String cStrings数组中的每个choices进行比较。

比较Strings时始终使用.equals()而不是==

示例:

if (c.equals(choices[0])) {
    // code
} else if (c.equals(choices[1])) {
    //code
} else if (c.equals(choices[2])) {
    // code
}