finished the ability to type in numbers with the keyboard.
This commit is contained in:
parent
c85a85e8f5
commit
02165bd184
|
@ -19,13 +19,8 @@ public class CalcController implements Initializable {
|
||||||
private String lastChar = "";
|
private String lastChar = "";
|
||||||
|
|
||||||
private void addChar(String next, boolean spacing) {
|
private void addChar(String next, boolean spacing) {
|
||||||
if(lastChar.equals("(") || lastChar.equals(")")) {
|
lastChar = next;
|
||||||
if(!next.matches("\\d")) {
|
this.label.setText(this.label.getText() + (spacing ? " " : "") + next + (spacing ? " " : ""));
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.label.setText((spacing ? " " : "") + next + (spacing ? " " : ""));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addChar(String next) {
|
private void addChar(String next) {
|
||||||
|
@ -100,18 +95,7 @@ public class CalcController implements Initializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onSum(ActionEvent actionEvent) {
|
public void onSum(ActionEvent actionEvent) {
|
||||||
Expression expression = new ExpressionBuilder(this.label.getText()).build();
|
calculate();
|
||||||
|
|
||||||
double result = expression.evaluate();
|
|
||||||
|
|
||||||
System.out.println(result % 1);
|
|
||||||
|
|
||||||
if(result % 1.0 == 0.0) {
|
|
||||||
int intResult = (int) result;
|
|
||||||
this.label.setText("" + intResult);
|
|
||||||
} else {
|
|
||||||
this.label.setText("" + result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onPlus(ActionEvent actionEvent) {
|
public void onPlus(ActionEvent actionEvent) {
|
||||||
|
@ -128,10 +112,27 @@ public class CalcController implements Initializable {
|
||||||
addChar(keyEvent.getText());
|
addChar(keyEvent.getText());
|
||||||
} else if(keyEvent.getText().matches("[+\\-*/]")) {
|
} else if(keyEvent.getText().matches("[+\\-*/]")) {
|
||||||
addChar(keyEvent.getText(), true);
|
addChar(keyEvent.getText(), true);
|
||||||
|
} else if(keyEvent.getCode() == KeyCode.ENTER) {
|
||||||
|
calculate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void calculate() {
|
||||||
|
Expression expression = new ExpressionBuilder(this.label.getText()).build();
|
||||||
|
|
||||||
|
double result = expression.evaluate();
|
||||||
|
|
||||||
|
System.out.println(result % 1);
|
||||||
|
|
||||||
|
if(result % 1.0 == 0.0) {
|
||||||
|
int intResult = (int) result;
|
||||||
|
this.label.setText("" + intResult);
|
||||||
|
} else {
|
||||||
|
this.label.setText("" + result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void onKeyPressed(KeyEvent keyEvent) {
|
public void onKeyPressed(KeyEvent keyEvent) {
|
||||||
System.out.println(keyEvent.getText());
|
System.out.println(keyEvent.getText());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user