基本的加法和减法选择

Ric*_*ard 2 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

tra*_*god 6

您可能想要查看==运算符和equals()方法之间的区别.

附录:很容易找到有关Java 比较方法的好信息; 这是一个有点难以找到一个很好的解释了为什么与通常是错的.String ==String

附录:

我是否使用另一个if语句?

你的if-then声明很好; 现在你需要将它扩展为if-then-else语句.请注意如何if-then在之后添加更多语句else.