首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java - tip计算器

java - tip计算器
EN

Stack Overflow用户
提问于 2016-05-06 11:37:39
回答 2查看 1.4K关注 0票数 0

我正在创建这个小费计算器。外观是可以的,但一旦我尝试按下计算提示,什么也不会发生。我不确定我需要做什么功能的小费或总数。如果有人能给我一些见解,我将不胜感激。

代码如下:

代码语言:javascript
复制
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.collections.*;
import javafx.scene.layout.*;
import javafx.event.*;
import javafx.stage.Stage;

// Allows the user to enter a meal amount and select a tip rate.
// When the claculate button is pressed the tip and total for the meal is displayed

public class tips extends Application {

    private TextField tfMeal = new TextField();
    private TextField tfTip = new TextField();
    private TextField tfTotal = new TextField();
    private ComboBox cbTips;
    private ObservableList<String> tip_rates =
            FXCollections.observableArrayList (
                    "0.05", "0.10",
                    "0.15", "0.18",
                    "0.20", "0.22",
                    "0.25", "0.30");

  @Override
  // Override the start method in the Application class
  public void start(Stage primaryStage) {

    VBox pane = new VBox(5);      

    tfMeal.setPrefColumnCount(10);
    tfTip.setPrefColumnCount(5);
    tfTotal.setPrefColumnCount(10);

// Compmo box for tip rates
    cbTips = new ComboBox(tip_rates);
    cbTips.setVisibleRowCount(4);
    cbTips.setValue(tip_rates.get(4));

    pane.getChildren().addAll(new Label("Amount: "), tfMeal, new Label("Tip rates: "), cbTips);


    HBox hBox = new HBox(5);
    Button btCalculate = new Button("Calculate Tip");

    hBox.setAlignment(Pos.CENTER);
    hBox.getChildren().addAll(btCalculate, new Label("Tip: "), tfTip, new Label("Total: "), tfTotal);
    // tip and total are display only fields
    tfTip.setEditable(false);
    tfTotal.setEditable(false);

    BorderPane borderPane = new BorderPane();
    borderPane.setCenter(pane);
    borderPane.setBottom(hBox);
    BorderPane.setAlignment(hBox, Pos.TOP_CENTER);

    // Create a scene and place it in the stage
    Scene scene = new Scene(borderPane, 375, 150);
    primaryStage.setTitle("Tip calculator"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage

    btCalculate.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent e) {
            // take the values in  tfMeal and cbTip
            // calculate and display in tfTip and tfTotal as the tip and total
            // for extra credit add exception handling (but do not display the error in the console)

            // this statement gets the tip rate
            double tiprt = Double.parseDouble(cbTips.getValue().toString());

        }
    });



  }

  /**
   * The main method is only needed for the IDE with limited
   * JavaFX support. Not needed for running from the command line.
   */
  public static void main(String[] args) {
    launch(args);
  }
} 

EN

回答 2

Stack Overflow用户

发布于 2016-05-06 11:42:32

您的程序正在按预期运行;您需要在此处添加功能。下面是实现的代码:

代码语言:javascript
复制
btCalculate.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent e) {
            // take the values in  tfMeal and cbTip
            // calculate and display in tfTip and tfTotal as the tip and total
            // for extra credit add exception handling (but do not display the error in the console)

            // this statement gets the tip rate
            double tiprt = Double.parseDouble(cbTips.getValue().toString());
            double tfMealVal = Double.parseDouble(tfMeal.getText().toString());
            double rslt = tfMealVal*tiprt;
            double totalAmnt = rslt+tfMealVal;
            tfTip.setText(Double.toString(rslt));
            tfTotal.setText(Double.toString(totalAmnt));

        }
    });

输出:

票数 0
EN

Stack Overflow用户

发布于 2016-05-06 11:45:41

实际上,您还没有尝试在处理程序中做任何有用的事情。你所需要的只是一个基本的数学计算。

要得到小费金额,您必须将小费金额乘以小费比率。

代码语言:javascript
复制
tipAmount = amount * tipRate;

要获得总金额,您必须将小费金额与原始金额相加。

代码语言:javascript
复制
total = amount + tipAmount;

然后,您需要使用这些新计算的金额设置相应文本字段的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37063795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档