2010-12-04 66 views
2

請幫忙。我對此很新,而且我非常需要這個。我需要創建一個程序,讓它可以選擇加法或減法。這是我目前的程序:基本加減選項

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

怎麼了?即使在第一步,我也無法得到它。

+0

重新格式化的代碼;如果不正確請回復。 – trashgod 2010-12-04 02:27:45

+1

請具體說明您的問題。 – 2010-12-04 02:29:30

回答

6

您可能想要查看==操作員與equals()方法之間的區別。

附錄:在Java上很容易找到好的信息Stringcomparison methods;爲什麼==String通常是錯誤的,找到good explanation有點困難。

附錄:

難道我if語句使用另一個?

你有一個很好的if-then聲明;現在您需要將其展開爲if-then-else聲明。注意如何在else之後添加更多if-then語句。