mandy 發問時間: 電腦與網際網路程式設計 · 8 年前


This exercise consists of two Java's programs (i.e., two versions); one is for

application, the other for applet. You are required to write a program, which gets two inputs (called operands) and one operator (can be either one of '+', '-', '*' and '/')

and then return the result of the operations (indicated by the operator) on the two

operands. For example, you input 3, 8, and /, and the program will reply 0.375.


Note that you must use the method "JOptionPane.showInputDialog" to get the

operands and operator, and use the method "JOptionPane.showInputDialog" to

show the result.



2 個已更新項目:

After you finish the exercise, you need to upload your source codes for the two versions (application and applet) through the e-learning system by the deadline of the exercise.

For the applet version, you also need to upload the HTML file that embeds the applet.

1 個解答

  • ?
    Lv 7
    8 年前


    import javax.swing.*;

    public class Y3583 {

    public static void main(String[] args) {

    double x = Double.parseDouble(JOptionPane.showInputDialog("Operand 1:"));

    double y = Double.parseDouble(JOptionPane.showInputDialog("Operand 1:"));

    String op = JOptionPane.showInputDialog("Operator(+, -, *, /):");

    double d = 0.0D;

    if (op.equals("+")) {

    d = x + y;

    } else if (op.equals("-")) {

    d = x - y;

    } else if (op.equals("*")) {

    d = x * y;

    } else if (op.equals("/")) {

    d = x / y;


    JOptionPane.showMessageDialog(null, x + op + y + "=" + d);