Follwong控制器中的布尔值是什么?
我正在学习 Java fx 初学者课程,我的任务是使用基于 MVC 模式的场景生成器创建一个简单的袖珍计算器。我理解模型和视图,但我不理解以下控制器中布尔值 isTypingNumber 的用途。 我的老师说这应该用于检查用户是否正在输入数字,以便他在此期间无法执行任何其他操作,但我不明白他的意思或在这种情况下应该意味着什么。也许有人可以在这里给我一个解释。
最好的问候〜
控制器:
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
public class Controller {
@FXML
Text resultLabel;
boolean isTypingNumber = false;
double firstNumber = 0;
double secondNumber = 0;
String operation = "";
Model model = new Model();
public void numberTapped(ActionEvent event) { //When user tapps on a number
String value = ((Button)event.getSource()).getText();
if (isTypingNumber) {
resultLabel.setText(resultLabel.getText() + value);
} else {
resultLabel.setText(value);
isTypingNumber = true;
}
}
public void calculationTapped(ActionEvent event) { //When User clicks a + / - / * or /
isTypingNumber = false;
firstNumber = Double.parseDouble(resultLabel.getText());
operation = ((Button) event.getSource()).getText();
}
public void equalsTapped(ActionEvent event) { // When click on =
isTypingNumber = false;
secondNumber = Double.parseDouble(resultLabel.getText());
double result = model.calculate(firstNumber, secondNumber, operation);
resultLabel.setText(String.valueOf(result));
}
public void restartTapped(ActionEvent event ){ //When click on "C"
resultLabel.setText("0");
firstNumber = 0;
secondNumber = 0;
}
}
这里的模型可能是为了更好地理解:
package application;
public class Model {
public double calculate(double number1, double number2, String operation) {
switch (operation) {
case "+":
return number1 + number2;
case "-":
return number1 - number2;
case "*":
return number1 * number2;
case "/":
if(number2 == 0) {
System.out.println("Durch 0 teilen geht nicht");
}
else {
return number1/number2;
}
}
return 0;
}
}
I'm doing a beginner's course on Java fx and I have the task of creating a simple pocket calculator with the Scene Builder based on the MVC pattern. I understand model and view, but I don't understand the purpose of the boolean isTypingNumber in the following controller.
My teacher says this should be used to check whether the user is typing in a number so that he cannot perform any other operations during this time, but I do not understand what he means by that or what that is supposed to mean in this context. Maybe someone can give me an explanation here.
Best Regards~
Controller:
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
public class Controller {
@FXML
Text resultLabel;
boolean isTypingNumber = false;
double firstNumber = 0;
double secondNumber = 0;
String operation = "";
Model model = new Model();
public void numberTapped(ActionEvent event) { //When user tapps on a number
String value = ((Button)event.getSource()).getText();
if (isTypingNumber) {
resultLabel.setText(resultLabel.getText() + value);
} else {
resultLabel.setText(value);
isTypingNumber = true;
}
}
public void calculationTapped(ActionEvent event) { //When User clicks a + / - / * or /
isTypingNumber = false;
firstNumber = Double.parseDouble(resultLabel.getText());
operation = ((Button) event.getSource()).getText();
}
public void equalsTapped(ActionEvent event) { // When click on =
isTypingNumber = false;
secondNumber = Double.parseDouble(resultLabel.getText());
double result = model.calculate(firstNumber, secondNumber, operation);
resultLabel.setText(String.valueOf(result));
}
public void restartTapped(ActionEvent event ){ //When click on "C"
resultLabel.setText("0");
firstNumber = 0;
secondNumber = 0;
}
}
And here the Model maybe for better understanding:
package application;
public class Model {
public double calculate(double number1, double number2, String operation) {
switch (operation) {
case "+":
return number1 + number2;
case "-":
return number1 - number2;
case "*":
return number1 * number2;
case "/":
if(number2 == 0) {
System.out.println("Durch 0 teilen geht nicht");
}
else {
return number1/number2;
}
}
return 0;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
布尔值
istypingnumber
用于确定是否应将输入的用户附加到resultlabel
中的文本中,或在result> resultlabel
中替换文本。假设用户要计算“ 22 + 33”:
isTypingNumber
是错误的,因此resultlabel中的文本
将被“ 2”替换为“ 2”,istypingnumber
设置为trueistypingnumber
是正确的,因此该数字将附加到resultlabel
中的文本上,该现在包含“ 22”。操作
,FirstNumber
中,将其更改为22,istypingnumber
设置为falseresullabel
中的文本是否应该通过附加“ 3”更改为“ 223”,还是应该通过更换内容来更改为“ 3”?istypingnumber
回答以下问题:它是错误的,因此resultlabel中的文本
将用“ 3”代替。在第4步中,需要
istypingnumber
变得很明显。如果您将
resultlabel
中的文本更改为第3步中的空字符串,则可能会删除istypingnumber
,但是IMHO会使用户界面尴尬:操作按钮您先前输入的号码将消失。这意味着,如果用户尝试进行多个计算(“ 22 + 33 =”,“ 3 * 15 =”),他将需要按下之间的重新启动按钮。The boolean
isTypingNumber
is used to decide whether a number that the user entered should be appended to the text inresultLabel
or replace the text inresultLabel
.Let's say the user wants to calculate "22 + 33":
isTypingNumber
is false, so the text inresultLabel
will be replaced with "2" andisTypingNumber
is set to trueisTypingNumber
is true, so the number is appended to the text inresultLabel
which now contains "22".operation
,firstNumber
is changed to 22 andisTypingNumber
is set to falseresultLabel
be changed to "223" by appending the "3" or should it be changed to "3" by replacing the contents?isTypingNumber
answers this question: it is false, so the text inresultLabel
will be replaced with "3".It is in that step number 4 where the need for
isTypingNumber
becomes obvious.You could possibly remove
isTypingNumber
if you changed the text inresultLabel
to an empty string in step 3, but IMHO that would make the user interface awkward: as soon as you press an operation button the number that you entered previously would disappear. And it would mean that if the user would try to do several calculations ("22 + 33 =", "3 * 15 =") he would need to press the restart button in between.