该程序执行以下操作:第一个输入从用户获得两个数字,第二个输入如果用户输入(+)符号,程序将两个数字相加,如果用户输入( - )符号,则从第二个数字中减去第一个数字.但没有结果显示该程序只是运行和终止.
import javax.swing.JOptionPane;
public class ObtainNumber {
public static void main(String []args) {
String strNum1 = JOptionPane.showInputDialog("Enter the first number");
String strNum2 = JOptionPane.showInputDialog("Enter the second number");
int num1 = Integer.parseInt(strNum1);
int num2 = Integer.parseInt(strNum2);
String askForOperation = JOptionPane.showInputDialog("What operation needs to be done?");
int sum;
if (askForOperation == "+") {
sum = num1 + num2;
JOptionPane.showMessageDialog(null, "The result of the Addition is " + sum);
}
double subtract;
if (askForOperation == "-") {
subtract = num2 - num1;
JOptionPane.showMessageDialog(null, "The result of the subtraction is " + subtract);
}
}
Run Code Online (Sandbox Code Playgroud)
}
if (askForOperation == "+") // Reference is not same
Run Code Online (Sandbox Code Playgroud)
比较检查使用equals方法.
if ("+".equals(askForOperation)) // Compare content
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |