Java FX显示等式的输出

时间:2017-02-02 19:24:38

标签: java

我正在制作一个简单的体重指数计算器。但是,每当我输入带小数点的数字(同时尝试使用,和。)时,它都不会显示结果。如果我输入一个像181这样的数字,结果就会显示出来。知道为什么会这样吗?

Main Class:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("BMI Calculator");
    primaryStage.setScene(new Scene(root, 600, 400));
    primaryStage.show();
}


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

控制器类:

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {
// Connecting components with Controller
@FXML
private TextField weightField;
@FXML
private TextField heightField;
@FXML
private TextField resField;
@FXML
private Button submitButton;

@FXML
    private void setSubmitButton (ActionEvent event){
    // Getting value from weight and height text fields

    Double userWeight = Double.parseDouble(weightField.getText());
    Double userHeight = Double.parseDouble(heightField.getText());
    Double result = userWeight / Math.pow(userHeight, 2);

    // Compare result and display relevant text
    if (result >= 27.5) {
    resField.setText("You are fat " + "\nYou scored: " + result.toString());
    }
    else if (result <=23 && result < 27.4) {
   resField.setText("You are obese" + "\nYou scored: " + result.toString());
    }
    else if (result <=18.5 && result <22.9) {
 resField.setText("You are normal" + "\n You scored: " + result.toString());
    }
    else if (result <18.4) {
 resField.setText("You are chudy" + "\n You scored: " + result.toString());
    }
}
@Override
public void initialize(URL location, ResourceBundle resources) {
        submitButton.setOnAction(this::setSubmitButton);
}
}

0 个答案:

没有答案