基本的加法和减法选择

时间:2010-12-04 02:21:25

标签: java class jcreator

请帮助解决这个问题。我对此很陌生,我非常需要这个。我需要创建一个程序,让你可以选择加法或减法。这是我目前的计划:

import javax.swing.JOptionPane;

public class RationalZ {

    public static void main(String args[]) {

        JOptionPane.showMessageDialog(null,
            "Enter the letter A for Addition and B Subtraction");

        String choice = JOptionPane.showInputDialog(
            "A=Addition B=Subtraction");

        if (choice == "a") {

            String strNum1 = JOptionPane.showInputDialog(
                "Enter a Number");
            String strNum2 = JOptionPane.showInputDialog(
                "Enter a second number");

            int aNum1 = Integer.parseInt(strNum1);
            int aNum2 = Integer.parseInt(strNum2);

            JOptionPane.showMessageDialog(null, aNum1 + aNum2);

            System.exit(0);

        } else {
            System.exit(0);
        }
    }
}

怎么了?即使是在第一步,我也无法得到它。

1 个答案:

答案 0 :(得分:6)

您可能希望查看==运算符和equals()方法之间的差异。

附录:在Java String comparison methods上找到好的信息相当容易;找good explanation== String的{​​{1}}通常是错误的。{/ p>

附录:

  

我是否使用其他if语句?

你有一个很好的if-then陈述;现在您需要将其扩展为if-then-else语句。请注意如何在else之后添加更多if-then语句。

相关问题