嘿伙计们我正在制作一个java应用程序来预订安装。我正在尝试设置一个If else语句,以使InstallType列打印出“标准”或“自定义” 它需要这样做的方法是:如果numOutlets变量大于4或者numZones大于2则应该打印出Custom。 如果numOutlets的数量小于或等于4或numZones中的数字等于或小于2,则应打印出“标准”。
我试图在enterButton上执行此操作:
public void enterButtonClicked(){
Installation installation = new Installation();
installation.setCustomerName(nameInput.getText());
installation.setHouseNumber(Double.parseDouble(houseInput.getText()));
installation.setStreetName(streetInput.getText());
installation.setTown(townInput.getText());
installation.setNumOutlets(Integer.parseInt(outletInput.getText()));
installation.setNumZones(Integer.parseInt(zoneInput.getText()));
installationTable.getItems().add(installation);
double numOutlets = Double.parseDouble(outletInput.getText());
double numZones = Double.parseDouble(zoneInput.getText());
if (numOutlets>=2 || numZones>=5)
installationType = "Custom";
else
installationType = "Standard";
installType.setText(InstallationType + "");
nameInput.clear();
houseInput.clear();
streetInput.clear();
townInput.clear();
outletInput.clear();
zoneInput.clear();
}
这是我的TableView
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("CQ Air-Conditioning");
//installationID
TableColumn<Installation, Integer> installationID = new TableColumn<>("Installation ID");
installationID.setMinWidth(100);
installationID.setCellValueFactory(new PropertyValueFactory<>("installationNumber"));
//CustomerName
TableColumn<Installation, String> nameColumn = new TableColumn<>("Name");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("customerName"));
//House Number
TableColumn<Installation, Double> houseNo = new TableColumn<>("House Number");
houseNo.setMinWidth(100);
houseNo.setCellValueFactory(new PropertyValueFactory<>("houseNumber"));
//Street Name
TableColumn<Installation, String> street = new TableColumn<>("Street Name");
street.setMinWidth(200);
street.setCellValueFactory(new PropertyValueFactory<>("streetName"));
//Town Name
TableColumn<Installation, String> Town = new TableColumn<>("Town Name");
Town.setMinWidth(200);
Town.setCellValueFactory(new PropertyValueFactory<>("town"));
//number outlets
TableColumn<Installation, Double> numberOutlets = new TableColumn<>("Outlets");
numberOutlets.setMinWidth(50);
numberOutlets.setCellValueFactory(new PropertyValueFactory<>("numOutlets"));
//number Zones
TableColumn<Installation, Double> numberZones = new TableColumn<>("Zones");
numberZones.setMinWidth(50);
numberZones.setCellValueFactory(new PropertyValueFactory<>("numZones"));
//Installation Type
TableColumn<Installation, String> installType = new TableColumn<>("Type of Installation");
installType.setMinWidth(200);
installType.setCellValueFactory(new PropertyValueFactory<>("installationType"));
//total cost
TableColumn<Installation, Double> cost = new TableColumn<>("Total Cost");
cost.setMinWidth(150);
cost.setCellValueFactory(new PropertyValueFactory<>("totalCost"));
//nameInput
nameInput = new TextField();
nameInput.setPromptText("Enter Name");
nameInput.setMinWidth(100);
//houseInput
houseInput = new TextField();
houseInput.setPromptText("enter house number");
//streetInput
streetInput = new TextField();
streetInput.setPromptText("enter street Name");
//town input
townInput = new TextField();
townInput.setPromptText("enter town");
//outlets input
outletInput = new TextField();
outletInput.setPromptText("enter number of outlets");
//zones input
zoneInput = new TextField();
zoneInput.setPromptText("enter number of zones");
//buttons
Button enterButton = new Button ("Enter");
enterButton.setOnAction(e -> enterButtonClicked());
enterButton.setMinWidth(200);
Button clearButton = new Button ("Clear");
clearButton.setOnAction(e -> clearButtonClicked());
clearButton.setMinWidth(50);
Button deleteButton = new Button ("Delete");
deleteButton.setOnAction(e -> deleteButtonClicked());
deleteButton.setMinWidth(50);
Button exitButton = new Button ("Exit");
exitButton.setOnAction(e -> exitButtonClicked());
exitButton.setMinWidth(50);
HBox hbox = new HBox();
hbox.setPadding(new Insets(25, 10, 50, 10));
hbox.setSpacing(10);
hbox.getChildren().addAll(nameInput, houseInput, streetInput, townInput, outletInput, zoneInput);
HBox buttons = new HBox();
buttons.setPadding(new Insets(10,10,10,10));
buttons.setSpacing(15);
buttons.setAlignment(Pos.CENTER);
buttons.getChildren().addAll(clearButton, enterButton, deleteButton, exitButton);
installationTable = new TableView<>();
installationTable.setItems(getInstallation());
installationTable.getColumns().addAll(installationID, nameColumn, houseNo, street, Town, numberOutlets, numberZones, installType, cost);
VBox vbox = new VBox();
vbox.getChildren().addAll(hbox,installationTable, buttons);
Scene scene = new Scene(vbox);
window.setScene(scene);
window.show();
}
我正在使用一个“Main”类和一个名为“Installation”的类,包含我的变量,getter和setter。
这是我在“安装”类中的变量
private String customerName;
private String streetName;
private String town;
private String installationType;
private double postCode;
private double houseNumber;
private int numZones;
private int numOutlets;
private double totalCost;
private double standardCost = 7200;
private double customCost;
private int installationNumber = 0;
感谢任何帮助我的人,我多年来一直在努力尝试,但似乎无法得到它。非常感谢!对不起,如果我没有正确发布,仍在努力学习。
答案 0 :(得分:0)
我想我看到了你的问题。这是您现在拥有的代码:
if (numOutlets>=2 || numZones>=5)
installationType = "Custom";
else
installationType = "Standard";
这是您需要将其更改为:
if (numZones>=2 || numOutlets>=5)
installationType = "Custom";
else
installationType = "Standard";
根据上面的代码,您可以切换变量。