【问题标题】:How to differentiate between first int and second int in a java calculator?如何区分java计算器中的第一个int和第二个int?
【发布时间】:2013-03-28 22:39:24
【问题描述】:

我正在使用 MVC 风格制作一个 java 计算器。我只是无法区分他们输入的第一个数字和第二个数字。我输入了一个布尔值,当输入加号或减号时变为真,这将数字字符串更改回“”,一个空白字符串。但是当我在这个布尔值为真时去获取它时,字符串已经是空白的,所以为时已晚。关于如何获得第一个数字然后是第二个数字的任何建议?这个计算器只是使用加号和减号。带有一个等于和清除按钮。谢谢

计算类

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


class Calc extends JFrame implements ActionListener {


    private JTextField numDisplay = new JTextField(10);
    private JButton plusButton = new JButton("+");
    private JButton minusButton = new JButton("-");
    private JButton clearButton = new JButton("Clear");
    private JButton equalsButton = new JButton("=");
    private JButton zeroButton = new JButton("0");
    private JButton oneButton = new JButton("1");
    private JButton twoButton = new JButton("2");
    private JButton threeButton = new JButton("3");
    private JButton fourButton = new JButton("4");
    private JButton fiveButton = new JButton("5");
    private JButton sixButton = new JButton("6");
    private JButton sevenButton = new JButton("7");
    private JButton eightButton = new JButton("8");
    private JButton nineButton = new JButton("9");
    private String number = "";
    private boolean trueFalse;   //plus or minus
    private boolean onOff = false;   //false = 1st int, true = 2nd int
    private int total;
    private boolean isEquals = false;   // false = haven't clicked equals button yet,     true they have
    //private String numberText;

    Calc(){
        JPanel calcPanel = new JPanel();

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400, 600);
        calcPanel.add(numDisplay);
        calcPanel.add(plusButton);
        calcPanel.add(minusButton);
        calcPanel.add(clearButton);
        calcPanel.add(equalsButton);
        calcPanel.add(zeroButton);
        calcPanel.add(oneButton);
        calcPanel.add(twoButton);
        calcPanel.add(threeButton);
        calcPanel.add(fourButton);
        calcPanel.add(fiveButton);
        calcPanel.add(sixButton);
        calcPanel.add(sevenButton);
        calcPanel.add(eightButton);
        calcPanel.add(nineButton);

        this.add(calcPanel);

        plusButton.addActionListener(this);
        minusButton.addActionListener(this);
        clearButton.addActionListener(this);
        equalsButton.addActionListener(this);
        zeroButton.addActionListener(this);
        oneButton.addActionListener(this);
        twoButton.addActionListener(this);
        threeButton.addActionListener(this);
        fourButton.addActionListener(this);
        fiveButton.addActionListener(this);
        sixButton.addActionListener(this);
        sevenButton.addActionListener(this);
        eightButton.addActionListener(this);
        nineButton.addActionListener(this);

    }
    public void actionPerformed(ActionEvent event){
        if (event.getSource() instanceof JButton){
            JButton clickedButton = (JButton) event.getSource();
            String buttonText = clickedButton.getText();
            if (clickedButton == zeroButton || clickedButton == oneButton || clickedButton == twoButton || clickedButton == threeButton || clickedButton == fourButton || clickedButton == fiveButton || clickedButton == sixButton || clickedButton == sevenButton || clickedButton == eightButton || clickedButton == nineButton)
            {
                number = number + buttonText;
            }
            if (clickedButton == clearButton){
                number = "";
                onOff = false;
            }
            if (clickedButton == plusButton){
                trueFalse = true;
                onOff = true;
            }
            if (clickedButton == minusButton){
                trueFalse = false;
                onOff = true;
                number = "";
            }
            if (clickedButton == equalsButton){
                isEquals = true;
            }
        }
    }
    public int getNumber(){
        return Integer.parseInt(number);
    }
    public boolean trueFalse(){
        return trueFalse;
    }
    public boolean onOff(){
        return onOff;
    }
    public int total(){
        return total;
    }
    public boolean isEquals(){
        return isEquals;
    }
    public String getNumberString(){
        return number;
    }
}

计算类

public class Calculations
{
    private Calc theView;
    private CalculationModel theModel;
    private boolean onOff = theView.onOff();
    private boolean trueFalse = theView.trueFalse();
    private int number1;

    public Calculations(Calc theView, CalculationModel theModel)
    {
        this.theView = theView;
        this.theModel = theModel;
    }

    public void doesometing() {
        if(trueFalse)     
        {
            number1 = theView.getNumber();
        }
    }   
}

【问题讨论】:

    标签: java swing model-view-controller user-interface calculator


    【解决方案1】:

    处理用户输入的一种方法是将用户输入的值压入堆栈。这样他最近的值是最后的。

    要进行计算,您需要从堆栈中弹出两个值,运行计算并将结果压入堆栈。 thn 输入的下一个值在最后一个结果之后进入堆栈。

    编辑:收集数字以生成数字可以通过将数字附加到字符串的末尾并在按下非数字时将字符串转换为数字来完成。请注意,连续的两个点不是有效数字,因此您需要处理它。

    【讨论】:

    • 对,但他们是点击按钮,而不是自己输入字符。对不起,我应该说得更清楚
    • 展示不重要,你会有价值观和运营。如果按钮按下导致数字,您需要收集这些数字,直到按下某个功能并从按钮按下时生成一个数字。
    • 那我该怎么做呢?我不确定如何在清除回空白之前获取该号码
    • 这是一个 MVC 项目,因此请考虑该模式的各个部分。按钮和值的显示是视图。按下按钮时执行的操作是控制器,包含值的输入字符串和堆栈是模型。按下的每个数字都会导致附加到模型中的下一个输入字符串的操作,并在视图中显示该字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    相关资源
    最近更新 更多