简单的基本计算器 Javafx

Simple basic Calculator Javafx

首先,这是我的第一个应用程序,我正在尝试编写计算器代码。当按下一个运算符时,如果有旧的,计算它并将结果发送给新的进程继续。计算过程不进行第二步,谁能帮忙让这段代码正常运行?

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class main extends Application {
    String num1 ="";
    String num2 ="";
    String op ;
    double result= 0;
    boolean oldop =false ;
    // the GUI component
    public void start(Stage stage) throws Exception {
        Button one = new Button("1");
        Button two = new Button("2");
        Button pls = new Button("+");
        Button eql = new Button("=");
        Button ac = new Button("AC");
        Label lbl = new Label("empty");
        FlowPane pane = new FlowPane();
        pane.setHgap(10);
        pane.getChildren().addAll(one,two,pls,eql,ac,lbl);

        Scene scene = new Scene(pane);
        stage.setScene(scene);
        stage.show();
        // The Actions on buttons
        one.setOnAction(e ->
            {
            if(!oldop){
                num1+='1';
            lbl.setText(num1);}
            else {
                num2+='1';
                lbl.setText(num2);}});

        two.setOnAction(e ->
        {
            if(!oldop){
                num1+='2';
                lbl.setText(num1);}
            else {
                num2+='2';
                lbl.setText(num2);}});

        pls.setOnAction(e -> {
            if(!oldop){
                oldop = true;
                op="+";
                lbl.setText(op);}
            else {
                result=calc(num1 , num2 ,op);
                num1=String.valueOf(result);
                num2="";
                op="+";
                lbl.setText(num1+op);
                oldop = true;}});

        eql.setOnAction(e ->{
            if(oldop){
                result=calc(num1 , num2 , op);
                lbl.setText(String.valueOf(result));
                oldop=false;
                num2="";}
            else 
                return;});

        ac.setOnAction(e -> {
            num1="";
            num2="";
            result=0;
            oldop=false;});

    }
    // The calculation method
    public int calc (String n1 , String n2 , String op){
        switch (op) {
        case "+" :
            return Integer.parseInt(n1) + Integer.parseInt(n2) ;
        case "-" :
            return Integer.parseInt(n1) - Integer.parseInt(n2) ;
        case "*" :
            return Integer.parseInt(n1) * Integer.parseInt(n2) ;
        case "/" :
            return Integer.parseInt(n1) / Integer.parseInt(n2) ;
        default :
            return 0;
       }
    }

public static void main(String[] args) {
    Application.launch(args);
}
}

问题似乎是您不能在第二步中使用先前操作的结果,因为您使用 String.valueOf 给出例如3.0 表示 int 3(1+2 的结果)。该字符串不能在 calc 中再次使用,因为它不能用 `Integer.parseInt 解析回 int。

我建议使用 int 并只将它们转换为标签的字符串。

一个糟糕的解决方法是在 calc 的开头添加以下行:

    n1=n1.split("\.")[0];
    n2=n2.split("\.")[0];