21xrx.com
2024-12-23 01:46:41 Monday
登录
文章检索 我的文章 写文章
「教程」利用Java编写简易计算器代码 让计算更方便!
2023-06-19 16:24:12 深夜i     --     --

今天我来教大家如何用Java编写一个简易计算器代码,让计算变得更加方便快捷。

首先,我们需要先了解计算器的基本功能,即加、减、乘、除四个基本运算。在Java中,可以用四个操作符表示这四个运算,加法为“+”,减法为“-”,乘法为“*”,除法为“/”。

接下来,我们需要为计算器创建一个界面。可以使用JavaFX或Swing等工具创建,这里以JavaFX为例:


import javafx.application.Application;

import javafx.geometry.Insets;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

import javafx.scene.layout.GridPane;

import javafx.stage.Stage;

public class Calculator extends Application {

  private TextField tfNum1 = new TextField();

  private TextField tfNum2 = new TextField();

  private TextField tfResult = new TextField();

  private Button btAdd = new Button("+");

  private Button btSubtract = new Button("-");

  private Button btMultiply = new Button("*");

  private Button btDivide = new Button("/");

  private Button btClear = new Button("清空");

  @Override

  public void start(Stage primaryStage) {

    GridPane gridPane = new GridPane();

    gridPane.setHgap(5);

    gridPane.setVgap(5);

    gridPane.setPadding(new Insets(5, 5, 5, 5));

    gridPane.add(new Label("数字1:"), 0, 0);

    gridPane.add(tfNum1, 1, 0);

    gridPane.add(new Label("数字2:"), 0, 1);

    gridPane.add(tfNum2, 1, 1);

    gridPane.add(new Label("结果:"), 0, 2);

    gridPane.add(tfResult, 1, 2);

    gridPane.add(btAdd, 0, 3);

    gridPane.add(btSubtract, 1, 3);

    gridPane.add(btMultiply, 0, 4);

    gridPane.add(btDivide, 1, 4);

    gridPane.add(btClear, 1, 5);

    btAdd.setOnAction(e -> add());

    btSubtract.setOnAction(e -> subtract());

    btMultiply.setOnAction(e -> multiply());

    btDivide.setOnAction(e -> divide());

    btClear.setOnAction(e -> clear());

    Scene scene = new Scene(gridPane);

    primaryStage.setScene(scene);

    primaryStage.setTitle("简易计算器");

    primaryStage.show();

  }

  private void add() {

    double num1 = Double.parseDouble(tfNum1.getText());

    double num2 = Double.parseDouble(tfNum2.getText());

    double result = num1 + num2;

    tfResult.setText(String.valueOf(result));

  }

  private void subtract() {

    double num1 = Double.parseDouble(tfNum1.getText());

    double num2 = Double.parseDouble(tfNum2.getText());

    double result = num1 - num2;

    tfResult.setText(String.valueOf(result));

  }

  private void multiply() {

    double num1 = Double.parseDouble(tfNum1.getText());

    double num2 = Double.parseDouble(tfNum2.getText());

    double result = num1 * num2;

    tfResult.setText(String.valueOf(result));

  }

  private void divide() {

    double num1 = Double.parseDouble(tfNum1.getText());

    double num2 = Double.parseDouble(tfNum2.getText());

    double result = num1 / num2;

    tfResult.setText(String.valueOf(result));

  }

  private void clear() {

    tfNum1.setText("");

    tfNum2.setText("");

    tfResult.setText("");

  }

  public static void main(String[] args) {

    launch(args);

  }

}

该代码创建了一个包含两个输入框、一个输出框和五个按钮的界面,分别表示两个数字、结果和四个基本运算,还包括一个清空按钮。

接下来我们需要为五个按钮分别添加事件,实现对结果的计算和清空操作。


btAdd.setOnAction(e -> add());

btSubtract.setOnAction(e -> subtract());

btMultiply.setOnAction(e -> multiply());

btDivide.setOnAction(e -> divide());

btClear.setOnAction(e -> clear());

...

private void add() {

  double num1 = Double.parseDouble(tfNum1.getText());

  double num2 = Double.parseDouble(tfNum2.getText());

  double result = num1 + num2;

  tfResult.setText(String.valueOf(result));

}

private void subtract() {

  double num1 = Double.parseDouble(tfNum1.getText());

  double num2 = Double.parseDouble(tfNum2.getText());

  double result = num1 - num2;

  tfResult.setText(String.valueOf(result));

}

private void multiply() {

  double num1 = Double.parseDouble(tfNum1.getText());

  double num2 = Double.parseDouble(tfNum2.getText());

  double result = num1 * num2;

  tfResult.setText(String.valueOf(result));

}

private void divide() {

  double num1 = Double.parseDouble(tfNum1.getText());

  double num2 = Double.parseDouble(tfNum2.getText());

  double result = num1 / num2;

  tfResult.setText(String.valueOf(result));

}

private void clear() {

  tfNum1.setText("");

  tfNum2.setText("");

  tfResult.setText("");

}

现在,我们用这个简易计算器代码进行计算时可以输入数字,然后点击 +、-、*、/符号进行对应运算,清空按钮可清空所有数字和结果。

最后,我们来总结一下本文讲解的关键词:

1. Java编程

2. 简易计算器

3. 计算基本运算

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复