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.

<BR>

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

    }

    }

還有問題?馬上發問,尋求解答。