-3

I have this code in javascript.

symbol = window.prompt( "Enter the symbol", "+" );

    if (symbol == "+")
        {
        result = value1 + value2;
        document.writeln( "<h1>The sum is " + sum + "</h1>" );
        }

     else if (symbol == "-")
        {
        result = value1 - value2;
        document.writeln( "<h1>The sub is " + sum + "</h1>" );
        }

     else if (symbol == "*")
        {
        result = value1 * value2;
        document.writeln( "<h1>The multiplication is " + sum + "</h1>" );
        }

     else if (symbol == "/")
        {
        result = value1 / value2;
        document.writeln( "<h1>The division is " + sum + "</h1>" );
        }

I want to convert it in Java. The user has already input two numbers and now he has to input one of 4 symbols (+, -, *, /) in order to do the additional arithmetical operation and get the result.

4

1 回答 1

3

可以JOptionPane.showInputDialog(...)用来模拟window.prompt()。其余的很简单。

示例代码:

import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class DemoJOptionPane {
    public static void main(String[] args) {
        double value1 = 5, value2 = 3, result;

        JDialog.setDefaultLookAndFeelDecorated(true);
        String symbol = JOptionPane.showInputDialog(null, "+-*/",
                "Enter the symbol", JOptionPane.OK_OPTION);
        if (symbol.equals("+")) {
            result = value1 + value2;
            System.out.println("<h1>The sum is " + result + "</h1>");
        } else if (symbol.equals("-")) {
            result = value1 - value2;
            System.out.println("<h1>The sub is " + result + "</h1>");
        } else if (symbol.equals("*")) {
            result = value1 * value2;
            System.out.println("<h1>The multiplication is " + result + "</h1>");
        } else if (symbol.equals("/")) {
            result = value1 / value2;
            System.out.println("<h1>The division is " + result + "</h1>");
        }
    }
}


在您的场景中可能更简单,如果您只想让用户输入信息,您也可以直接在控制台中要求输入。

下面的示例代码将请求控制台中的输入,其行为方式与前面的代码相同。

示例代码(从控制台输入):

import java.util.Scanner;
public class DemoScanner {
    public static void main(String[] args) {
        double value1 = 5, value2 = 3, result;

        Scanner in = new Scanner(System.in);
        System.out.print("Enter the symbol (+-*/): ");
        String symbol = in.next().substring(0, 1);
        in.close();

        if (symbol.equals("+")) {
            result = value1 + value2;
            System.out.println("<h1>The sum is " + result + "</h1>");
        } else if (symbol.equals("-")) {
            result = value1 - value2;
            System.out.println("<h1>The sub is " + result + "</h1>");
        } else if (symbol.equals("*")) {
            result = value1 * value2;
            System.out.println("<h1>The multiplication is " + result + "</h1>");
        } else if (symbol.equals("/")) {
            result = value1 / value2;
            System.out.println("<h1>The division is " + result + "</h1>");
        }
    }
}
于 2013-07-01T03:24:06.050 回答