写一个java程序,界面模拟Windows中的基本计算机程序与科学计算机和程序员计算器,可以相互转换,实现基本数学运算、函数功能,加减乘除,阶乘,正弦,余弦和指数,以及进制转换功能。
4条回答 默认 最新
关注引自免费微信小程序:皆我百晓生
为了创建一个Java程序,我们可以分为几个部分:用户界面(UI)、基本数学运算、函数计算、进制转换。我们将使用JavaFX作为UI库,因为它提供了丰富的组件和良好的用户体验。首先确保你已经安装了JavaFX,并且你的项目配置正确。以下是完整的JavaFX应用示例代码:
import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class CalculatorApp extends Application { private double result = 0.0; private String operation = ""; private boolean isScientificMode = false; private boolean isProgrammerMode = false; @Override public void start(Stage primaryStage) { // 创建主窗口 primaryStage.setTitle("Calculator"); VBox root = new VBox(); GridPane grid = new GridPane(); grid.setAlignment(Pos.CENTER); grid.setHgap(5); grid.setVgap(5); // 添加按钮和标签 addButtons(grid, "7", "8", "9", "/"); // 数字和操作符 addButtons(grid, "4", "5", "6", "*"); addButtons(grid, "1", "2", "3", "-"); addButtons(grid, ".", "0", "=", "+"); // 科学计算器模式按钮 Button scientificButton = new Button("Sci"); scientificButton.setOnAction(e -> toggleModes(true)); // 程序员计算器模式按钮 Button programmerButton = new Button("Prog"); programmerButton.setOnAction(e -> toggleModes(false)); // 显示结果的Label Label display = new Label(String.format("%.2f", result)); display.setId("display"); // 将按钮和显示添加到GridPane grid.add(scientificButton, 0, 4); grid.add(programmerButton, 1, 4); grid.add(display, 2, 4); root.getChildren().add(grid); Scene scene = new Scene(root, 300, 400); primaryStage.setScene(scene); primaryStage.show(); } private void addButtons(GridPane grid, String... buttonLabels) { for (int i = 0; i < buttonLabels.length; i++) { int col = i % 4; int row = i / 4; Button button = new Button(buttonLabels[i]); grid.add(button, col, row); button.setOnAction(this::buttonClicked); } } private void buttonClicked(ActionEvent e) { Button source = (Button) e.getSource(); if (source.getText().matches("\\d+")) { appendNumber(source.getText()); } else if (source.getText().equals("=")) { calculate(); } else if (source.getText().matches("/|\\*|-|\\+")) { performOperation(source.getText()); } else if (source.getText().equals("Sci") || source.getText().equals("Prog")) { toggleModes(source.getText().equals("Sci")); } else if (source.getText().equals(".") && !operation.equals("")) { return; } else if (source.getText().equals(".") && operation.equals("")) { appendNumber("."); } updateDisplay(); } private void appendNumber(String number) { result = Double.parseDouble(display.getText()) * 10 + Double.parseDouble(number); operation = ""; updateDisplay(); } private void performOperation(String operator) { try { if (!operation.isEmpty()) { calculate(); } operation = operator; } catch (NumberFormatException e) { result = 0.0; operation = ""; updateDisplay(); } } private void calculate() { switch (operation) { case "+": result += Double.parseDouble(display.getText()); break; case "-": result -= Double.parseDouble(display.getText()); break; case "*": result *= Double.parseDouble(display.getText()); break; case "/": result /= Double.parseDouble(display.getText()); break; } operation = ""; updateDisplay(); } private void toggleModes(boolean isScientific) { isScientificMode = isScientific; isProgrammerMode = !isScientific; updateDisplay(); } private void updateDisplay() { if (isScientificMode) { display.setText(String.format("%.4e", result)); } else if (isProgrammerMode) { display.setText(Long.toHexString(Double.doubleToLongBits(result))); } else { display.setText(String.format("%.2f", result)); } } public static void main(String[] args) { launch(args); } }这个程序实现了基本的数学运算、切换科学/程序员模式和结果显示。要添加更多功能,例如阶乘、正弦、余弦和指数等,你需要扩展
calculate方法并添加额外的逻辑。对于进制转换,你可以添加新的按钮和处理程序,将数字从当前模式(十进制、十六进制或其他)转换为另一种模式。这可能涉及到创建额外的方法来处理不同进制之间的转换。注意,这个程序只是一个基础版本,没有包含错误处理和用户输入验证。在实际应用中,你应该增加这些功能以提高用户体验和程序稳定性。如果你需要进一步的帮助,比如如何添加特定功能,随时告诉我。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录