-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelloController.java
77 lines (60 loc) · 1.86 KB
/
HelloController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.example.calculator;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class HelloController {
@FXML
private TextField txt_result;
String op = "";
long number1;
long number2;
public void Delete(ActionEvent ae) {
String del = ((Button) ae.getSource()).getText();
if (!del.isEmpty()) {
txt_result.clear();
}
}
public void Number(ActionEvent ae) {
String no = ((Button) ae.getSource()).getText();
txt_result.setText(txt_result.getText() + no);
}
public void Operation(ActionEvent ae) {
String operation = ((Button) ae.getSource()).getText();
if (!operation.equals("=")) {
if (!op.equals("")) {
return;
}
op = operation;
number1 = Long.parseLong(txt_result.getText());
txt_result.setText("");
} else {
if (op.equals("")) {
return;
}
number2 = Long.parseLong(txt_result.getText());
calculate(number1, number2, op);
op = "";
}
}
public void calculate(long n1, long n2, String op) {
switch (op) {
case "+":
txt_result.setText(n1 + n2 + "");
break;
case "-":
txt_result.setText(n1 - n2 + "");
break;
case "*":
txt_result.setText(n1 * n2 + "");
break;
case "/":
if (n2 == 0) {
txt_result.setText("0");
break;
}
txt_result.setText(n1 / n2 + "");
break;
}
}
}