Follwong控制器中的布尔值是什么?

发布于 2025-01-18 09:12:34 字数 2611 浏览 0 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月蹉跎了容颜 2025-01-25 09:12:34

布尔值istypingnumber用于确定是否应将输入的用户附加到resultlabel中的文本中,或在result> resultlabel中替换文本。

假设用户要计算“ 22 + 33”:

  1. 用户按“ 2”。 isTypingNumber是错误的,因此resultlabel中的文本将被“ 2”替换为“ 2”,istypingnumber设置为true
  2. 用户再次按“ 2”。 。 istypingnumber是正确的,因此该数字将附加到resultlabel中的文本上,该现在包含“ 22”。
  3. 用户按“+”。将其存储在操作FirstNumber中,将其更改为22,istypingnumber设置为false
  4. 用户按“ 3”。 resullabel中的文本是否应该通过附加“ 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 in resultLabel or replace the text in resultLabel.

Let's say the user wants to calculate "22 + 33":

  1. the user presses "2". isTypingNumber is false, so the text in resultLabel will be replaced with "2" and isTypingNumber is set to true
  2. the user presses "2" again. isTypingNumber is true, so the number is appended to the text in resultLabel which now contains "22".
  3. the user presses "+". This is stored in operation, firstNumber is changed to 22 and isTypingNumber is set to false
  4. the user presses "3". Should the text in resultLabel 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 in resultLabel 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 in resultLabel 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文