java實現(xiàn)計算器模板及源碼
計算器實現(xiàn)了大部分基礎(chǔ)功能:基本運算,菜單欄選項,并且拓展了普通型和科學(xué)興選項等等,讀者可以在此基礎(chǔ)上進(jìn)行修改和拓展。其他具體實現(xiàn)方法可以看源碼,里面有詳細(xì)的概述,代碼框架清晰。
運行環(huán)境:win10 Eclipse IDE for Java Developers - 2020-06
下面是計算器的視圖:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /* ?* 計算器 ?*/ public class CaculatorTest implements ActionListener { ?? ?// 初始框架搭建 ?? ?JFrame frame = new JFrame("計算器"); ?? ?JTextField area = new JTextField("0"); ?? ?JPanel panel1 = new JPanel(); ?? ?JPanel panel2 = new JPanel(); ?? ?JButton[] buttons = new JButton[20]; ?? ?String[] buttonsText = { "sqrt", "退格", "C", "/", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", "0", ?? ??? ??? ?".", "+/-", "=" }; ?? ?boolean point = false; // 用于判斷是否輸入多位小數(shù)點 ?? ?boolean key = true; // 做完運算("=")后繼續(xù)輸入數(shù)字 ?? ?String sign = " "; // 用于判斷和記錄運算符號 ?? ?double temp = 0; // 多次連續(xù)運算時,值的寄存處 ?? ?public CaculatorTest() { ?? ??? ?initMenu(); ?? ??? ?initText(); ?? ??? ?initExtend(); ?? ??? ?initFrame(); ?? ??? ?initBorderLayout(); ?? ?} ?? ?// 初始化菜單 ?? ?private void initMenu() { ?? ??? ?JMenuBar mb = new JMenuBar(); ?? ??? ?JMenu m1 = new JMenu("選項"); ?? ??? ?JMenu m2 = new JMenu("編輯"); ?? ??? ?JMenu m3 = new JMenu("幫助"); ?? ??? ?JMenuItem m11 = new JMenuItem("普通型計算器"); ?? ??? ?JMenuItem m12 = new JMenuItem("科學(xué)型計算器"); ?? ??? ?m1.add(m11); ?? ??? ?m1.add(m12); ?? ??? ?m11.addActionListener(new ActionListener() { ?? ??? ??? ?@Override ?? ??? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ??? ?boolean flag = false; ?? ??? ??? ??? ?panel2.setVisible(flag); ?? ??? ??? ??? ?frame.pack(); ?? ??? ??? ?} ?? ??? ?}); ?? ??? ?m12.addActionListener(new ActionListener() { ?? ??? ??? ?@Override ?? ??? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ??? ?boolean flag = true; ?? ??? ??? ??? ?panel2.setVisible(flag); ?? ??? ??? ??? ?frame.pack(); ?? ??? ??? ?} ?? ??? ?}); ?? ??? ?mb.add(m1); ?? ??? ?mb.add(m2); ?? ??? ?mb.add(m3); ?? ??? ?frame.setJMenuBar(mb); ?? ?} ?? ?// 初始化輸出文本域 ?? ?private void initText() { ?? ??? ?area.setFont(new Font("TimesRoman", Font.PLAIN, 20)); ?? ??? ?area.setSize(400, 100); ?? ??? ?area.setHorizontalAlignment(JTextField.RIGHT); // 向右顯示 ?? ?} ?? ?// 初始化拓展功能 ?? ?private void initExtend() { ?? ??? ?panel2.setLayout(new GridLayout(1,4,1,1)); ?? ??? ?JButton b1 = new JButton("sin"); ?? ??? ?JButton b2 = new JButton("cos"); ?? ??? ?JButton b3 = new JButton("exp"); ?? ??? ?JButton b4 = new JButton("ln"); ?? ??? ?b1.setFont(new Font("TimesRoman", Font.PLAIN, 20)); ?? ??? ?b2.setFont(new Font("TimesRoman", Font.PLAIN, 20)); ?? ??? ?b3.setFont(new Font("TimesRoman", Font.PLAIN, 20)); ?? ??? ?b4.setFont(new Font("TimesRoman", Font.PLAIN, 20)); ?? ??? ?b1.setSize(100, 100); ?? ??? ?b1.addActionListener(this); ?? ??? ?b2.setSize(100, 100); ?? ??? ?b2.addActionListener(this); ?? ??? ?b3.setSize(100, 100); ?? ??? ?b3.addActionListener(this); ?? ??? ?b4.setSize(100, 100); ?? ??? ?b4.addActionListener(this); ?? ??? ?panel2.add(b1); ?? ??? ?panel2.add(b2); ?? ??? ?panel2.add(b3); ?? ??? ?panel2.add(b4); ?? ?} ?? ?// 初始化計算器基本界面 ?? ?private void initFrame() { ?? ??? ?panel1.setLayout(new GridLayout(5, 4, 1, 1)); ?? ??? ?for (int i = 0; i < buttonsText.length; i++) { ?? ??? ??? ?JButton button = new JButton(buttonsText[i]); ?? ??? ??? ?button.setSize(100, 100); ?? ??? ??? ?button.setFont(new Font("TimesRoman", Font.PLAIN, 20)); ?? ??? ??? ?button.addActionListener(this); ?? ??? ??? ?panel1.add(button); ?? ??? ?} ?? ?} ?? ?// 初始化計算器總基本界面 ?? ?private void initBorderLayout() { ?? ??? ?frame.setLayout(new BorderLayout()); ?? ??? ?frame.add(panel1, BorderLayout.SOUTH); // 插入組件 ?? ??? ?frame.add(area, BorderLayout.NORTH); ?? ??? ?frame.add(panel2, BorderLayout.CENTER); ?? ??? ?frame.setLocation(700, 400); ?? ??? ?frame.setSize(400, 700); ?? ??? ?frame.setVisible(true); // 設(shè)置可見 ?? ??? ?panel2.setVisible(false); ?? ??? ?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 可以關(guān)閉 ?? ??? ?frame.pack(); ?? ?} ?? ?public static void main(String[] args) { ?? ??? ?new CaculatorTest(); ?? ?} ?? ?@Override ?? ?// 事件監(jiān)聽 ?? ?public void actionPerformed(ActionEvent e) { ?? ??? ?String str = e.getActionCommand(); ?? ??? ?String str2 = area.getText(); ?? ??? ?if (str == "0" || str == "1" || str == "2" || str == "3" || str == "4" || str == "5" || str == "6" || str == "7" ?? ??? ??? ??? ?|| str == "8" || str == "9") { ?? ??? ??? ?if (key == false) { ?? ??? ??? ??? ?area.setText(str2 + str); ?? ??? ??? ?} else { ?? ??? ??? ??? ?area.setText(str); ?? ??? ??? ??? ?key = false; ?? ??? ??? ?} ?? ??? ?} else if (str == "C") { ?? ??? ??? ?area.setText("0"); ?? ??? ??? ?sign = " "; ?? ??? ??? ?key = true; ?? ??? ?} else if (str == ".") { ?? ??? ??? ?if (point == false) { ?? ??? ??? ??? ?area.setText(str2 + str); ?? ??? ??? ??? ?point = true; ?? ??? ??? ?} else { ?? ??? ??? ??? ?area.setText("double poits!press C to update!"); ?? ??? ??? ??? ?point = false; ?? ??? ??? ?} ?? ??? ?} else if (str == "+/-") { ?? ??? ??? ?double num = Double.valueOf(str2); ?? ??? ??? ?num = -num; ?? ??? ??? ?area.setText(String.valueOf(num)); ?? ??? ?} else if (str == "退格") { ?? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ?area.setText("can't be deleted!please press C!"); ?? ??? ??? ?} else { ?? ??? ??? ??? ?str2 = str2.substring(0, str2.length() - 1); ?? ??? ??? ??? ?area.setText(str2); ?? ??? ??? ?} ?? ??? ?} else if (str == "sqrt") { ?? ??? ??? ?area.setText(""); ?? ??? ??? ?sign = "s"; ?? ??? ?} else if (str == "sin") { ?? ??? ??? ?area.setText(""); ?? ??? ??? ?sign = "sin"; ?? ??? ?} else if (str == "cos") { ?? ??? ??? ?area.setText(""); ?? ??? ??? ?sign = "cos"; ?? ??? ?} else if (str == "exp") { ?? ??? ??? ?area.setText(""); ?? ??? ??? ?sign = "exp"; ?? ??? ?} else if (str == "ln") { ?? ??? ??? ?area.setText(""); ?? ??? ??? ?sign = "ln"; ?? ??? ?} else { ?? ??? ??? ?if (str == "+") { ?? ??? ??? ??? ?if (sign == " ") { ?? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?temp = Double.valueOf(str2); ?? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ?} else if (sign == "-") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp - Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "+") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp + Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "*") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp * Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "/") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} else if (Double.valueOf(str2) == 0) { ?? ??? ??? ??? ??? ??? ?area.setText("除數(shù)不能為0哦!按 C"); ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp / Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "s") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.sqrt(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "sin") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.sin(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "cos") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.cos(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "exp") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.exp(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "ln") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.log(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "+"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} else if (str == "-") { ?? ??? ??? ??? ?if (sign == " ") { ?? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?temp = Double.valueOf(str2); ?? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ?} else if (sign == "-") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp - Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "+") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp + Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "*") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp * Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "/") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} else if (Double.valueOf(str2) == 0) { ?? ??? ??? ??? ??? ??? ?area.setText("除數(shù)不能為0哦!按 C"); ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp / Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "s") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.sqrt(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "sin") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.sin(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "cos") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.cos(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "exp") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.exp(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "ln") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.log(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "-"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} else if (str == "*") { ?? ??? ??? ??? ?if (sign == " ") { ?? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?temp = Double.valueOf(str2); ?? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ?} else if (sign == "-") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp - Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "+") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp + Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "*") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp * Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "/") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} else if (Double.valueOf(str2) == 0) { ?? ??? ??? ??? ??? ??? ?area.setText("除數(shù)不能為0哦!按 C"); ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp / Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "s") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.sqrt(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "sin") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.sin(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "cos") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.cos(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "exp") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.exp(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "ln") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.log(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "*"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} else if (str == "/") { ?? ??? ??? ??? ?if (sign == " ") { ?? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?temp = Double.valueOf(str2); ?? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ?} else if (sign == "-") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp - Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "+") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp + Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "*") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp * Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "/") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} else if (Double.valueOf(str2) == 0) { ?? ??? ??? ??? ??? ??? ?area.setText("除數(shù)不能為0哦!按 C"); ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp / Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ??? ?key = true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "s") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.sqrt(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "sin") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.sin(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "cos") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.cos(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "exp") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.exp(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "ln") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Math.log(Double.valueOf(str2)); ?? ??? ??? ??? ??? ??? ?area.setText(""); ?? ??? ??? ??? ??? ??? ?sign = "/"; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} else if (str == "=") { ?? ??? ??? ??? ?if (sign == "+") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp + Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "-") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp - Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "*") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp * Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "/") { ?? ??? ??? ??? ??? ?if (Double.valueOf(str2) == 0) { ?? ??? ??? ??? ??? ??? ?area.setText("除數(shù)不能為0哦!按C"); ?? ??? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = temp / Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == " ") { ?? ??? ??? ??? ??? ?if (str2.length() == 0) { ?? ??? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?temp = Double.valueOf(str2); ?? ??? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else if (sign == "s") { ?? ??? ??? ??? ??? ?temp = Math.sqrt(Double.valueOf(str2)); ?? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ?} else if (sign == "sin") { ?? ??? ??? ??? ??? ?temp = Math.sin(Double.valueOf(str2)); ?? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ?} else if (sign == "cos") { ?? ??? ??? ??? ??? ?temp = Math.cos(Double.valueOf(str2)); ?? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ?} else if (sign == "exp") { ?? ??? ??? ??? ??? ?temp = Math.exp(Double.valueOf(str2)); ?? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ?} else if (sign == "ln") { ?? ??? ??? ??? ??? ?temp = Math.log(Double.valueOf(str2)); ?? ??? ??? ??? ??? ?area.setText(String.valueOf(temp)); ?? ??? ??? ??? ??? ?sign = " "; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?key = true; ?? ??? ??? ?} ?? ??? ?} ?? ?} }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java多線程并發(fā)編程 Volatile關(guān)鍵字
volatile 關(guān)鍵字是一個神秘的關(guān)鍵字,也許在 J2EE 上的 JAVA 程序員會了解多一點,但在 Android 上的 JAVA 程序員大多不了解這個關(guān)鍵字。只要稍了解不當(dāng)就好容易導(dǎo)致一些并發(fā)上的錯誤發(fā)生,例如好多人把 volatile 理解成變量的鎖2017-05-05Java實現(xiàn)無損Word轉(zhuǎn)PDF的示例代碼
本文將利用Java中的兩個jar包:pdfbox和aspose-words實現(xiàn)無損Word轉(zhuǎn)PDF功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動手嘗試一下2022-06-06java中Date和Timestamp類型的相互轉(zhuǎn)換方式
這篇文章主要介紹了java中Date和Timestamp類型的相互轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07顯示IntelliJ IDEA工具的Run Dashboard功能圖文詳解
這篇文章主要介紹了顯示IntelliJ IDEA工具的Run Dashboard功能,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07