0

클래스 사이에 MVC 스타일 형식을 사용하여 Java에서 계산기 GUI 스타일을 만들어야합니다. 입력 한 첫 번째 숫자와 두 번째 숫자를 구별하려고합니다. true/false boolean이므로 참이면 숫자가 첫 번째 것이고 false이면 새 숫자임을 인식하고 이미 더하기 또는 빼기를 눌렀습니다. 부울을 반환하는 public 부울 메서드를 사용하지만 Calculations 클래스에있는 if 문이 어떤 이유로 작동하지 않습니다. 여기에 코드Java MVC 스타일 계산기에 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(); 
    } 



} 

답변

1

당신은 클래스 블록에서 사용할 수있는

private int number1; 

private 키워드 변수 클래스 멤버로 number1를 선언해야합니다. 또한,

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

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

를 교체하는 방법에 넣습니다.

은 참조 : Declaring Member Variables

0

당신이 널 포인터 예외가 있나요? 내가이의 모르겠지만

private boolean trueFalse = false; 

편집에 4 선을 계산 생성자

trueFalse = theView.trueFalse() 

이를 추가하고 편집하려고 : 및 생성자

전에 숫자 1을 선언