Java MVC style calculator is having an issue with an if statement

谁说胖子不能爱 提交于 2019-12-12 03:54:45

问题


I have to make a calculator GUI style in java with MVC style formatting between classes, I'm trying to differentiate between the first number they enter and the second one by having a true/false boolean, so if it's true then the number is the first one, and if its false then it should recognize that it is a new number, and they have already pressed either plus or minus. I use a public boolean method that returns the boolean but the if statement I have in the Calculations class isn't working for some reason. Here is the code

Calc class

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;

    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();
            number = number + buttonText;
            if (clickedButton == clearButton){
                number = "";
                onOff = false;
            }
            if (clickedButton == plusButton){
                trueFalse = true;
                onOff = true;
            }
            if (clickedButton == minusButton){
                trueFalse = false;
                onOff = true;
                number = "";
            }
        }
    }
    public int getNumber(){
        return Integer.parseInt(number);
    }
    public boolean trueFalse(){
        return trueFalse;
    }
    public boolean onOff(){
        return onOff;
    }
    public int total(){
        return total;
    }
}    

And here is the Calculations class where I'm having the issue.

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

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

    if(trueFalse == true)     //this if statement isn't working
    {
        private int number1 = theView.getNumber();
    }



}

回答1:


You need to declare number1 as a class member variable

private int number1;

The private keyword can only be used in the class block. Also, replace

if (trueFalse == true)     
{
   private int number1 = theView.getNumber(); 
}

with

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

and place it in a method.

See: Declaring Member Variables




回答2:


do you have a null pointer exception ?? i'm not sure of this but try to add this in the calculation constructor

trueFalse = theView.trueFalse()

and edit the 4th line to

private boolean trueFalse = false;

Edit: and declare number1 before constructor



来源:https://stackoverflow.com/questions/15672521/java-mvc-style-calculator-is-having-an-issue-with-an-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!