计算器不会回答新问题

时间:2015-06-02 07:12:16

标签: java jpanel jlabel

我正在处理的计算器将完美地解决第一个问题,但是当第二次点击添加时,答案会更接近加数,加数和总和都保持不变。任何人都可以通过这种方式对我的计划采取什么行动有所启发吗?谢谢!

public class GUI extends JFrame implements ActionListener{

private JTextField field1;
private JTextField field2;
private JButton add, subtract, multiply, divide;
private JLabel lanswer, label1, label2;
private String input1, input2, sanswer;
private int answer = 0;

JPanel contentPanel, answerPanel;

public GUI(){
    super("Calculator");

    field1 = new JTextField(null, 15);
    field2 = new JTextField(null, 15);

    add = new JButton("add");
    subtract = new JButton("subtract");
    multiply = new JButton("multiply");
    divide = new JButton("divide");

    lanswer = new JLabel("", SwingConstants.CENTER);
    label1 = new JLabel("Value 1:");
    label2 = new JLabel("Value 2:");

    add.addActionListener(this);

    Dimension opSize = new Dimension(110, 20);
    Dimension inSize = new Dimension(200, 20);

    lanswer.setPreferredSize(new Dimension(200,255));

    field1.setPreferredSize(inSize);
    field2.setPreferredSize(inSize);
    add.setPreferredSize(opSize);
    subtract.setPreferredSize(opSize);
    multiply.setPreferredSize(opSize);
    divide.setPreferredSize(opSize);

    contentPanel = new JPanel();
    contentPanel.setBackground(Color.PINK);
    contentPanel.setLayout(new FlowLayout());

    answerPanel = new JPanel();
    answerPanel.setPreferredSize(new Dimension(230, 260));
    answerPanel.setBackground(Color.WHITE);
    answerPanel.setLayout(new BoxLayout(answerPanel, BoxLayout.Y_AXIS));

    contentPanel.add(answerPanel);
    contentPanel.add(label1); contentPanel.add(field1); 
    contentPanel.add(label2); contentPanel.add(field2);
    contentPanel.add(add); contentPanel.add(subtract); contentPanel.add(multiply); contentPanel.add(divide);


    this.setContentPane(contentPanel);
}

public void actionPerformed(ActionEvent e) {
    JButton src = (JButton)e.getSource();

    if(src.equals(add)){
        add();
    } 
}

private void add(){
    input1 = field1.getText();
    input2 = field2.getText();

    MathEquation problem = new MathEquation(input1, input2);

    Dimension lineSize = new Dimension(200, 10);

    JPanel line1 = new JPanel(); line1.setBackground(Color.WHITE); 
    line1.setPreferredSize(lineSize);
    JPanel line2 = new JPanel(); line2.setBackground(Color.WHITE); 
    line2.setPreferredSize(lineSize);
    JPanel line3 = new JPanel(); line3.setBackground(Color.WHITE); 
    line3.setPreferredSize(lineSize);
    JPanel line4 = new JPanel(); line4.setBackground(Color.WHITE); 
    line4.setPreferredSize(lineSize);
    JPanel line5 = new JPanel(); line5.setBackground(Color.WHITE); 

    JLabel[] sumLabels = problem.getSumLabels();
    JLabel[] addend1Labels = problem.getAddend1Labels();
    JLabel[] addend2Labels = problem.getAddend2Labels();
    JLabel[] carriedLabels = problem.getCarriedLabels();

    for(int i = 0; i < carriedLabels.length; i++){
        line1.add(carriedLabels[i]);
    }

    for(int i = 0; i < addend1Labels.length; i++){
        line2.add(addend1Labels[i]);
    }

    for(int i = 0; i < addend2Labels.length; i++){
        line3.add(addend2Labels[i]);
    }

    String answerLine = "__";

    for(int i = 0; i < sumLabels.length; i++){
        answerLine += "__";
    }

    line4.add(new JLabel(answerLine));

    for(int i = 0; i < sumLabels.length; i++){
        line5.add(sumLabels[i]);
    }

    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));
    answerPanel.add(line1);
    answerPanel.add(line1);
    answerPanel.add(line2);
    answerPanel.add(line3);
    answerPanel.add(line4);
    answerPanel.add(line5);
    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));

    this.setContentPane(contentPanel);
    this.revalidate();
}

private String subtract(){
    input1 = field1.getText();
    input2 = field2.getText();
    answer = Integer.parseInt(input1) - Integer.parseInt(input2);

    sanswer = "<html><p align='right'>" + input1 + "<br>-+";

    if(input1.length() >= input2.length()){
        for(int i = 0; i < input1.length() - input2.length() + 1; i++){
            sanswer+="&nbsp;&nbsp;&nbsp;";
        }
    }

    sanswer += input2 + "<br>";

    if(input1.length() >= input2.length()){
        for(int i = 0; i < input1.length(); i++){
            sanswer+="--";
        }
    }
    else
    {
        for(int i = 0; i < input2.length(); i++){
            sanswer+="--";
        }
    }



    sanswer += "<br>" + answer + "</p></html>";

    return sanswer;

}

private String multiply(){
    input1 = field1.getText();
    input2 = field2.getText();
    answer = Integer.parseInt(input1) * Integer.parseInt(input2);

    sanswer = "<html><p align='right'>" + input1 + "<br>x";

    if(input1.length() >= input2.length()){
        for(int i = 0; i < input1.length() - input2.length() + 1; i++){
            sanswer+="&nbsp;&nbsp;&nbsp;";
        }
    }

    sanswer += input2 + "<br>";

    if(input1.length() >= input2.length()){
        for(int i = 0; i < input1.length(); i++){
            sanswer+="--";
        }
    }
    else
    {
        for(int i = 0; i < input2.length(); i++){
            sanswer+="--";
        }
    }



    sanswer += "<br>" + answer + "</p></html>";

    return sanswer;

}
}

MathEquation:

 public class MathEquation {

private ArrayList<Character> addend1, addend2, sum, carried;

public MathEquation(String a, String b){

    setAddends(a, b);
    findSum();

    System.out.println(carried.toString());
    System.out.println("   "+addend1.toString());
    System.out.println("   "+addend2.toString());
    System.out.println("   "+sum.toString());

}

public JLabel[] getAddend1Labels(){

    Dimension addendSize = new Dimension(10,10);
    JLabel[] getAddend1 = new JLabel[addend1.size()];

    for(int i = 0; i < addend1.size(); i++){
        getAddend1[i] = new JLabel(addend1.get(i).toString());
        getAddend1[i].setPreferredSize(addendSize);
    }

    return getAddend1;

}

public JLabel[] getAddend2Labels(){

    Dimension addendSize = new Dimension(10,10);
    JLabel[] getAddend2 = new JLabel[addend2.size()];

    for(int i = 0; i < addend2.size(); i++){
        getAddend2[i] = new JLabel(addend2.get(i).toString());
        getAddend2[i].setPreferredSize(addendSize);
    }

    return getAddend2;

}

public JLabel[] getSumLabels(){

    Dimension sumSize = new Dimension(10,10);
    JLabel[] getSum = new JLabel[sum.size()];

    for(int i = 0; i < sum.size(); i++){
        getSum[i] = new JLabel(sum.get(i).toString());
        getSum[i].setPreferredSize(sumSize);
    }

    return getSum;

}

public JLabel[] getCarriedLabels(){

    Dimension carriedSize = new Dimension(10,10);
    JLabel[] getCarried = new JLabel[carried.size()];

    for(int i = 0; i < carried.size(); i++){
        getCarried[i] = new JLabel(carried.get(i).toString());
        getCarried[i].setPreferredSize(carriedSize);
    }

    return getCarried;

}

public void setAddends(String a, String b){

    char[] arrayA = a.toCharArray();
    char[] arrayB = b.toCharArray();

    addend1 = new ArrayList<Character>();
    addend2 = new ArrayList<Character>();

    ArrayList wholeA = new ArrayList();
    ArrayList wholeB = new ArrayList();

    boolean wholeNumber = true;

    ArrayList decimalA = new ArrayList();
    ArrayList decimalB = new ArrayList();

    int decALength = 0, decBLength = 0;

    for(int i = 0; i < arrayA.length; i++){

        if(arrayA[i] != '.'){

            addend1.add(arrayA[i]);

            if(wholeNumber){

                wholeA.add(arrayA[i]);

            } else {

                decimalA.add(arrayA[i]);

            }

        } else {

            addend1.add(arrayA[i]);
            wholeNumber = false;    
            decALength = Arrays.copyOfRange(arrayA, i+1, arrayA.length).length;
            System.out.println(decALength);
        }

    }

    wholeNumber = true;

    for(int i = 0; i < arrayB.length; i++){

        if(arrayB[i] != '.'){

            addend2.add(arrayB[i]);

            if(wholeNumber){

                wholeB.add(arrayB[i]);

            } else {

                decimalB.add(arrayB[i]);

            }

            } else {

                addend2.add(arrayB[i]);
                wholeNumber = false;    
                decBLength = Arrays.copyOfRange(arrayB, i+1, arrayB.length).length;
                System.out.println(decBLength);
            }

    }

    if(decALength>decBLength){
        if(decBLength==0){
            addend2.add('.');
        }
        for(int i = 0; i < decALength-decBLength; i++){
            addend2.add('0');
        }
    } else if(decBLength>decALength){
        if(decALength==0){
            addend1.add('.');
        }
        for(int i = 0; i < decBLength-decALength; i++){
            addend1.add('0');
        }
    }

    if(wholeA.size() < wholeB.size())
        for(int i = 0; i < wholeB.size()-wholeA.size(); i++)
            addend1.add(0, '0');
    else if(wholeA.size() > wholeB.size())
        for(int i = 0; i < wholeA.size()-wholeB.size(); i++)
            addend2.add(0, '0');

    addend1.add(0, '0');
    addend2.add(0, '0');
}

private void findSum(){

    sum = new ArrayList<Character>();
    carried = new ArrayList<Character>();
    int dec = 0;

    carried.add('0');

    for(int i = addend1.size()-1; i >= 0; i--){

        if(addend1.get(i) != '.'){
            int a = Character.getNumericValue(addend1.get(i));
            int b = Character.getNumericValue(addend2.get(i));
            int c = Character.getNumericValue(carried.get(carried.size()-1));
            int d = a + b + c;

            if(d >= 10){
                carried.add('1');
                d-=10;
                sum.add((char) ('0' + d));
            } else {

                carried.add('0');
                sum.add((char) ('0' + d));

            } 

        } else {

            sum.add('.');
            carried.add(carried.size()-1, ' ');
        } 

    }

    Collections.reverse(sum);
    Collections.reverse(carried);

    carried.remove(0);

    for(int i = 0; i < carried.size(); i++){
        if(carried.get(i) == '0'){
            carried.set(i, ' ');
        }
    }

    for(int i = 0; i < addend1.size(); i++){
        if(addend1.get(i) == '0'){
            addend1.set(i, ' ');
        }
        else break;
    }

    for(int i = 0; i < addend2.size(); i++){
        if(addend2.get(i) == '0'){
            addend2.set(i, ' ');
        }
        else break;
    }

    for(int i = 0; i < sum.size(); i++){
        if(sum.get(i) == '0'){
            sum.set(i, ' ');
        }
        else break;
    }
}

}

0 个答案:

没有答案