Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MVC风格的计算器在if语句中出现问题_Java_Swing_Model View Controller_User Interface_Calculator - Fatal编程技术网

Java MVC风格的计算器在if语句中出现问题

Java MVC风格的计算器在if语句中出现问题,java,swing,model-view-controller,user-interface,calculator,Java,Swing,Model View Controller,User Interface,Calculator,我必须用java制作一个计算器GUI样式,在类之间使用MVC样式的格式,我试图通过一个真/假布尔值来区分他们输入的第一个数字和第二个数字,因此如果它是真的,那么这个数字就是第一个,如果它是假的,那么它应该识别它是一个新的数字,他们已经按下了加号或减号。我使用一个公共布尔方法返回布尔值,但由于某种原因,我在Calculations类中的if语句不起作用。这是密码 计算类 import javax.swing.*; import java.awt.*; import java.awt.event.*

我必须用java制作一个计算器GUI样式,在类之间使用MVC样式的格式,我试图通过一个真/假布尔值来区分他们输入的第一个数字和第二个数字,因此如果它是真的,那么这个数字就是第一个,如果它是假的,那么它应该识别它是一个新的数字,他们已经按下了加号或减号。我使用一个公共布尔方法返回布尔值,但由于某种原因,我在Calculations类中的if语句不起作用。这是密码

计算类

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;
    }
}    
这是我遇到问题的计算课

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();
    }



}

您需要将
number1
声明为类成员变量

private int number1;
private
关键字只能在类块中使用。另外,替换

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

并将其放在一个方法中


请参阅:

是否存在空指针异常?? 我不确定这一点,但尝试将其添加到计算构造函数中

trueFalse = theView.trueFalse()
并将第4行编辑为

private boolean trueFalse = false;
编辑:并在构造函数之前声明number1

trueFalse = theView.trueFalse()