【问题标题】:How can I convert this javascript code to Java? [closed]如何将此 javascript 代码转换为 Java? [关闭]
【发布时间】:2013-06-28 03:03:33
【问题描述】:

我在 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>" );
        }

我想用 Java 转换它。 用户已经输入了两个数字,现在他必须输入 4 个符号(+、-、*、/)中的一个,才能进行额外的算术运算并得到结果。

【问题讨论】:

  • 使用 javaFX 还是 swing?
  • 你尝试了什么?你有什么问题?
  • 我目前正在使用notepad++,我只想将此javascript代码转换为Java。我遇到的麻烦是我对Java不太熟悉。另外我不知道我必须将此行转换为什么 symbol = window.prompt("Enter the symbol", "+" );
  • 我对 JavaScript 了解不多,但如果我不得不猜测 window.prompt 很可能是一个 JDialog 弹出窗口。在 JDialog 中,您可以放置​​ JLabel、JTextField 和 JButton 以进行确认。希望这有助于您入门!或者,如果您不需要 GUI,您可以在命令终端中执行此操作,只需使用 System.out.println("Enter the symbol...");
  • 这正是 window.prompt 所做的。我唯一需要的是在 Java 中得到相同的结果,即使没有 JDialog 弹出窗口。我只想获取用户输入的符号。好吧,我有 'thisvalue1 = (new Integer(in.readLine())).intValue();'要从用户那里获取第一个数字,我只是不知道如何获取符号,因为它不是整数。

标签: java javascript symbols


【解决方案1】:

您可以使用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>");
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-11-21
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多