簡體   English   中英

我使用java創建了一個GUI計算器,看起來變量並沒有保持最新狀態,只是保持在0

[英]I've created a GUI calculator using java and it appears the variables aren't staying up to date and just remain at 0

我知道可能有更好的方法讓這個程序做它的功能但是我無法想出一個更好的算法來確切地完成這個程序的工作,任何建議都有幫助

package calc;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
*
* @author Ben
*/
public class GUI extends JFrame {

int response, count=0;    
double num1, num2, num3, num5, num6, num7, num8, total=0;
String operation, answer, num, testnum;
private JButton one, two, three, four, five, six, seven, eight, 
        nine, zero, multiply, divide, subtract, add, equals, clear;
private JTextField display, fakedisplay;

public GUI(){
    super("Calculator");
    setLayout (new FlowLayout());

    fakedisplay = new JTextField(10);
    display = new JTextField(10);
    display.setEditable(false);        
    add(display);                  
    one = new JButton("1");     
    add(one);
    two = new JButton("2");
    add(two);
    three = new JButton("3");
    add(three);
    four = new JButton("4");
    add(four);
    five = new JButton("5");
    add(five);
    six = new JButton("6");
    add(six);
    seven = new JButton("7");
    add(seven);
    eight = new JButton("8");
    add(eight);
    nine = new JButton("9");
    add(nine);
    zero = new JButton("0");
    add(zero);               
    multiply = new JButton("*");
    add(multiply);        
    divide = new JButton("/");
    add(divide);
    subtract = new JButton("-");
    add(subtract);
    add = new JButton("+");
    add(add);
    equals = new JButton("=");
    add(equals);  
    clear = new JButton("Clear");
    add(clear);


    handler handle = new handler();

    one.addActionListener(handle);
    two.addActionListener(handle);
    three.addActionListener(handle);
    four.addActionListener(handle);
    five.addActionListener(handle);
    six.addActionListener(handle);
    seven.addActionListener(handle);
    eight.addActionListener(handle);
    nine.addActionListener(handle);
    zero.addActionListener(handle);
    multiply.addActionListener(handle);
    divide.addActionListener(handle);
    subtract.addActionListener(handle);
    add.addActionListener(handle);
    equals.addActionListener(handle);
    clear.addActionListener(handle);

}
private class handler implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e){

        if(e.getSource()==one){
            response = 1;               
            display.setText(display.getText() + response);
            fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==two){
            response = 2;
            display.setText(display.getText() + response);      
            fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==three){
                response = 3;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==four){
                response = 4;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==five){
                response = 5;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==six){
                response = 6;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==seven){
                response = 7;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==eight){
                response = 8;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==nine){
                response = 9;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if(e.getSource()==zero){
            response = 0;
            display.setText(display.getText() + response);
            fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==multiply){
            if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }                
            else if(count == 1){
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1*num2;
                count++;
            }
            else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total*num3;
                count++;
            }
            operation = "*";
             display.setText(display.getText() + operation);
             fakedisplay.setText("");

        }
        else if (e.getSource()==divide){
             if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }
             else if(count == 1){
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1/num2+num1%num2;
                count++;
            }
             else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total/num3+total%num3;
                count++;
            }
                operation = "/";                    
                display.setText(display.getText() + operation);
                fakedisplay.setText("");
        }
        else if (e.getSource()==add){
            if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }
            else if(count == 1){
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1+num2;
                count++;
            }
            else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total+num3;
                count++;
            }
                operation = "+";
                display.setText(display.getText() + operation);
                fakedisplay.setText("");
        }
        else if (e.getSource()==subtract){
             if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }
             else if (count == 1) {
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1-num2;
                count++;
            }
             else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total-num3;
                count++;
            }
                operation = "-";
                display.setText(display.getText() + operation);
                fakedisplay.setText("");
        }
        else if (e.getSource()==equals){                
            operation = "=";
            display.setText(display.getText() + operation + total);                        
        }
        else if (e.getSource()==clear){
            display.setText("");
        }


}



}
}

我看到了一些問題,但您需要首先幫助您:

你在哪里進行平等操作? 做一個簡單的算術功能:

A op B = ?  

(其中A和B是數字,op是+, - ,*,/中的一個)

將一個數字插入num1 ,但不會執行op操作,因為count永遠不會等於1。

為了幫助自己,請在最后一個else if塊之后添加以下內容:

System.out.println("num1: " + num1 + " num2: " + num2 + " num3: " + num3
   + "\ndisplay: " + display.getText() + " fakedisplay: " + fakedisplay.getText() 
   + "\nresponse: " + response + "\ncount: " + count + "\ntotal: " + total);

這會給你一個關於你按下每個鍵/按鈕后剛剛發生了什么的提示。

如果您考慮邏輯步驟,您需要做的是:

Create fields for leftValue, operator, rightValue.
Handle the input just entered
   if input is a number and leftValue is null 
       leftValue = input
   if input is a number and leftValue (and operator) are not null
       leftValue = leftValue OPERATION input
   if input is an operator and leftValue is not null
       operator = input

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM