简体   繁体   English

基本加减选择

[英]Basic addition and subtraction with choices

Please help with this. 请帮忙。 I am very new to this, and I need this badly. 我对此很陌生,我非常需要这个。 I need to create a program where it lets you choose from addition or subtraction. 我需要创建一个程序,让您从加法或减法中进行选择。 This is my current program: 这是我当前的程序:

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);
        }
    }
}

What's wrong? 怎么了? Even in the first step I can't get it. 即使是第一步,我也无法理解。

You might want to review the difference between the == operator and the equals() method. 您可能需要查看==运算符和equals()方法之间的区别。

Addendum: It's fairly easy to find good information on Java String comparison methods ; 附录:在Java String 比较方法中找到好的信息相当容易; it's a little harder to find a good explanation of why == with String is usually wrong. 很难找到一个很好的解释 ,为什么用String ==通常是错误的。

Addendum: 附录:

Do I use another if statement? 我是否要使用另一个if语句?

You've got a good if-then statement; 您有一个很好的if-then语句; now you need to expand it to an if-then-else statement. 现在,您需要将其扩展为if-then-else语句。 Notice how you can add more if-then statements after the else . 注意如何在else后面添加更多if-then语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM