您的位置首页百科知识

JAVA 编写计算器 要代码最简单的

JAVA 编写计算器  要代码最简单的

学java的时候自己编的,很简单,能够连续输入计算式后进行计算import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.NumberFormat;import java.util.ArrayList;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;/**简易芦差隐计陪厅算器,能够进行简单的计算 * * @see 2008.12.9*/public class CalculatorA implements ActionListener{ private JFrame frame; private JTextField field; private JButton[] allButtons; private JButton clearButton;// private JButton backButton; String result="";//保存结果 StringBuilder sb = new StringBuilder();//保存要进行的计算式 int x = 0; //用来判断上一次的事件类型 String str = "123+456-789*0.=/"; ArrayList arrayList = new ArrayList();//保存计算式,通过方法进行运算 public CalculatorA(){ frame = new JFrame("我的计算庆好器v1.1"); frame.setLocation(300,300); field = new JTextField(25); allButtons = new JButton[16]; for(int i=0;i1){ result = result.substring(0,result.length()-1); if(sb.length()>0){ sb.delete(sb.length()-1,sb.length()); } else { sb.delete(0,1); } field.setText(result); x = 5; } else{ result = ""; sb.delete(0,sb.length()); arrayList.clear(); field.setText("0"); x = 0; } } } public static ArrayList getResult(ArrayList list){ String res = null; String[] s = {"/","*","-","+"}; int i=0; if(list.size()>1){ for(;i= '0' && c[k]<= '9')|| c[k] == '.'){ s_temp += c[k]; } } return s_temp; } public static void main(String[] args){ new CalculatorA().showMi(); }}