Java实现负/正按钮状态机计算器

时间:2012-11-30 04:26:44

标签: java calculator state-machine

我试图实现我的否定/肯定按钮,所以如果有人想输入负数,他们可以,但我的代码有运行时错误。

似乎陷入了enterState6()

我似乎无法让它突破状态6继续计算我需要某种循环吗?

package javacalculator;

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

public class JavaCalculator extends JFrame implements ActionListener
{
    private final int ADD = 10;
    private final int SUB = 11;
    private final int MUL = 12;
    private final int DIV = 13;
    private final int EQL = 14;
    private final int CLR = 15;
    private final int NEG = 16;
    private final int ERR = -1;


    private JTextField display; 
    private int state = 0;
    private double m0, m1;
    private int op;


    private JButton createButton(String tag)
    {
        JButton button = new JButton(tag);
        button.setActionCommand(tag);
        button.addActionListener(this);
        return button;
    }

    public JavaCalculator()
    {
        display = new JTextField("0.0");
        display.setHorizontalAlignment(JTextField.RIGHT);
        display.setEditable(false);
        display.setFont(new Font(display.getFont().getFontName(), display.getFont().getStyle(), 23));

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(6,6,3,3));
        panel.setBorder(BorderFactory.createEmptyBorder(16, 16, 16, 16));

        panel.add(createButton("7"));
        panel.add(createButton("8"));
        panel.add(createButton("9"));
        panel.add(createButton("+"));
        panel.add(createButton("4"));
        panel.add(createButton("5"));
        panel.add(createButton("6"));
        panel.add(createButton("-"));
        panel.add(createButton("1"));
        panel.add(createButton("2"));
        panel.add(createButton("3"));
        panel.add(createButton("*"));
        panel.add(createButton("0"));
        panel.add(createButton("="));
        panel.add(createButton("C"));
        panel.add(createButton("/"));
        panel.add(createButton("-/+"));

        getContentPane().add(display, BorderLayout.NORTH);
        getContentPane().add(panel, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Badass Calculator");
        setSize(300, 300);
        setVisible(true);
    }

    /**************************************************************************************************/

    private int convertEvent(ActionEvent event)
    {
        if(event.getActionCommand().equals("0"))
            return 0;
        if(event.getActionCommand().equals("1"))
            return 1;
        if(event.getActionCommand().equals("2"))
            return 2;
        if(event.getActionCommand().equals("3"))
            return 3;
        if(event.getActionCommand().equals("4"))
            return 4;
        if(event.getActionCommand().equals("5"))
            return 5;
        if(event.getActionCommand().equals("6"))
            return 6;
        if(event.getActionCommand().equals("7"))
            return 7;
        if(event.getActionCommand().equals("8"))
            return 8;
        if(event.getActionCommand().equals("9"))
            return 9;
        if(event.getActionCommand().equals("+"))
            return ADD;
        if(event.getActionCommand().equals("-"))
            return SUB;
        if(event.getActionCommand().equals("*"))
            return MUL;
        if(event.getActionCommand().equals("/"))
            return DIV;
        if(event.getActionCommand().equals("="))
            return EQL;
        if(event.getActionCommand().equals("C"))
            return CLR;
        if(event.getActionCommand().equals("-/+"))
            return NEG;


        return ERR;
    }

    private boolean isNumber(int i)
    {
        return i >= 0 && i <= 9;
    }

    private boolean isOperation(int i)
    {
        switch(i)
        {
            case ADD:
            case SUB:
            case MUL:
            case DIV: return true;
        }
        return false;
    }

    /***********************************************************************************************/

    private void enterState0(int key)
    {
        state = 0;
        m0 = 0;
        display.setText(Double.toString(m0));

    }

    private void enterState1(int key)
    {
        state = 1;
        m0 = m0 * 10 + key;
        display.setText(Double.toString(m0));

    }

    private void enterState2(int key)
    {
        state = 2;
        op = key;
        m1 = 0;
    }

    private void enterState3(int key)
    {
        state = 3;
        m1 = m1 * 10 + key;
        display.setText(Double.toString(m1));

    }

    private void enterState4(int key)
    {
        state = 4;
        switch(op)
        {
        case ADD: m1 = m0 + m1; break;
        case SUB: m1 = m0 - m1; break;
        case MUL: m1 = m0 * m1; break;
        case DIV: m1 = m0 / m1; break;
        }
        display.setText(Double.toString(m1));
        m0 = 0;
    }

    private void enterState5(int key)
    {
        state = 5;
        switch(op)
        {
        case ADD: m0 = m0 + m1; break;
        case SUB: m0 = m0 - m1; break;
        case MUL: m0 = m0 * m1; break;
        case DIV: m0 = m0 / m1; break;
        }
        display.setText(Double.toString(m0));
        m1 = 0;
        op = key;
    }

    private void enterState6(int key)
    {
        state = 6;

        m0 = m0 * (-1);

        display.setText(Double.toString(m0));
        op = key;
        m1 = 0;

    }

    public void actionPerformed(ActionEvent event)
    {
        int key = convertEvent(event);

        switch(state)
        {
        case 0:
            if(isNumber(key))
                enterState1(key);
            else if(key == NEG)
                enterState6(key);
            return;
        //number    
        case 1:
            if(key == CLR)
                enterState0(key);
            else if(isNumber(key))
                enterState1(key);
            else if(isOperation(key))
                enterState2(key);
            else if(key == NEG)
                enterState6(key);
            return;
        //operator  
        case 2:
            if(key == CLR)
                enterState0(key);
            else if(isNumber(key))
                enterState3(key);
            return;


        //number
        case 3:
            if(key == CLR)
                enterState0(key);
            else if(isNumber(key))
                enterState3(key);
            else if(key == EQL)
                enterState4(key);
            else if(key == op)
                enterState5(op);
            else if(key == NEG)
                enterState6(key);
            return;
        //equals    
        case 4: 
            if(key == CLR)
                enterState0(key);
            else if(isNumber(key))
                enterState1(key);
            else if(key == op)
                enterState5(op);
            return;
        //equals + another number   
        case 5:
            if(key == CLR)
                enterState0(key);
            else if(isNumber(key))
                enterState3(key);
            else if(key == NEG)
                enterState6(key);
            return;
        }
    }

    /************************************************************************************/

    public static void main(String[] args)
    {
        new JavaCalculator();
    }
}

// negative numbers needs its own state m0 = m1 * -1

2 个答案:

答案 0 :(得分:2)

您可以使用此功能将模式从正更改为负,反之亦然。单击“+/-”按钮时调用该函数。

public void NegPos() {
    int num=Integer.parseInt(TextBox.getText());
    if(num>=0) {
    int resultneg=num*-1;
    TextBox.setText(resultneg+"");
    }else
    {
        int resultpos=num*-1;
        TextBox.setText(resultpos+""); 

    }
}

TextBox 是对象。 希望我有所帮助

答案 1 :(得分:0)

switch方法中的actionPerformed语句包含以下情况:

        case 0:
        case 1:
        case 2:
        case 3:
        case 4: 
        case 5:

特别是case 6:。因此,一旦state变为6,就不会再发生任何事情了。