1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問(wèn)題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
      java命令行計(jì)算器代碼,java實(shí)現(xiàn)計(jì)算器代碼

      java 計(jì)算器代碼

      import javax.swing.*;

      為建平等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及建平網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、建平網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

      import javax.swing.border.Border;

      import java.awt.*;

      import java.awt.event.ActionListener;

      import java.awt.event.ActionEvent;

      import java.math.BigDecimal;

      import java.math.RoundingMode;

      import java.util.HashMap;

      /**

      * 我的計(jì)算器。Cheshi 繼承于 JFrame,是計(jì)算器的界面

      c*/

      public class Cheshi extends JFrame {

      private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);

      private JTextField textbox = new JTextField("0");

      private CalculatorCore core = new CalculatorCore();

      private ActionListener listener = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

      JButton b = (JButton) e.getSource();

      String label = b.getText();

      String result = core.process(label);

      textbox.setText(result);

      }

      };

      public Cheshi(String title) throws HeadlessException {

      super(title); // 調(diào)用父類構(gòu)造方法

      setupFrame(); // 調(diào)整窗體屬性

      setupControls(); // 創(chuàng)建控件

      }

      private void setupControls() {

      setupDisplayPanel(); // 創(chuàng)建文本面板

      setupButtonsPanel(); // 創(chuàng)建按鈕面板

      }

      // 創(chuàng)建按鈕面板并添加按鈕

      private void setupButtonsPanel() {

      JPanel panel = new JPanel();

      panel.setBorder(border);

      panel.setLayout(new GridLayout(4, 5, 3, 3));

      createButtons(panel, new String[]{

      "7", "8", "9", "+", "C",

      "4", "5", "6", "-", "CE",

      "1", "2", "3", "*", "", // 空字符串表示這個(gè)位置沒(méi)有按鈕

      "0", ".", "=", "/", ""

      });

      this.add(panel, BorderLayout.CENTER);

      }

      /**

      * 在指定的面板上創(chuàng)建按鈕

      *

      * @param panel 要?jiǎng)?chuàng)建按鈕的面板

      * @param labels 按鈕文字

      */

      private void createButtons(JPanel panel, String[] labels) {

      for (String label : labels) {

      // 如果 label 為空,則表示創(chuàng)建一個(gè)空面板。否則創(chuàng)建一個(gè)按鈕。

      if (label.equals("")) {

      panel.add(new JPanel());

      } else {

      JButton b = new JButton(label);

      b.addActionListener(listener); // 為按鈕添加偵聽(tīng)器

      panel.add(b);

      }

      }

      }

      // 設(shè)置顯示面板,用一個(gè)文本框來(lái)作為計(jì)算器的顯示部分。

      private void setupDisplayPanel() {

      JPanel panel = new JPanel();

      panel.setLayout(new BorderLayout());

      panel.setBorder(border);

      setupTextbox();

      panel.add(textbox, BorderLayout.CENTER);

      this.add(panel, BorderLayout.NORTH);

      }

      // 調(diào)整文本框

      private void setupTextbox() {

      textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右對(duì)齊

      textbox.setEditable(false); // 文本框只讀

      textbox.setBackground(Color.white); // 文本框背景色為白色

      }

      // 調(diào)整窗體

      private void setupFrame() {

      this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 當(dāng)窗體關(guān)閉時(shí)程序結(jié)束

      this.setLocation(100, 50); // 設(shè)置窗體顯示在桌面上的位置

      this.setSize(300, 200); // 設(shè)置窗體大小

      this.setResizable(false); // 窗體大小固定

      }

      // 程序入口

      public static void main(String[] args) throws Exception {

      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

      Cheshi frame = new Cheshi("我的計(jì)算器");

      frame.setVisible(true); // 在桌面上顯示窗體

      }

      }

      /**

      * 計(jì)算器核心邏輯。這個(gè)邏輯只能處理 1~2 個(gè)數(shù)的運(yùn)算。

      */

      class CalculatorCore {

      private String displayText = "0"; // 要顯示的文本

      boolean reset = true;

      private BigDecimal number1, number2;

      private String operator;

      private HashMapString, Operator operators = new HashMapString, Operator();

      private HashMapString, Processor processors = new HashMapString, Processor();

      CalculatorCore() {

      setupOperators();

      setupProcessors();

      }

      // 為每種命令添加處理方式

      private void setupProcessors() {

      processors.put("[0-9]", new Processor() {

      public void calculate(String command) {

      numberClicked(command);

      }

      });

      processors.put("\\.", new Processor() {

      public void calculate(String command) {

      dotClicked();

      }

      });

      processors.put("=", new Processor() {

      public void calculate(String command) {

      equalsClicked();

      }

      });

      processors.put("[+\\-*/]", new Processor() {

      public void calculate(String command) {

      operatorClicked(command);

      }

      });

      processors.put("C", new Processor() {

      public void calculate(String command) {

      clearClicked();

      }

      });

      processors.put("CE", new Processor() {

      public void calculate(String command) {

      clearErrorClicked();

      }

      });

      }

      // 為每種 operator 添加處理方式

      private void setupOperators() {

      operators.put("+", new Operator() {

      public BigDecimal process(BigDecimal number1, BigDecimal number2) {

      return number1.add(number2);

      }

      });

      operators.put("-", new Operator() {

      public BigDecimal process(BigDecimal number1, BigDecimal number2) {

      return number1.subtract(number2);

      }

      });

      operators.put("*", new Operator() {

      public BigDecimal process(BigDecimal number1, BigDecimal number2) {

      return number1.multiply(number2);

      }

      });

      operators.put("/", new Operator() {

      public BigDecimal process(BigDecimal number1, BigDecimal number2) {

      return number1.divide(number2, 30, RoundingMode.HALF_UP);

      }

      });

      }

      // 根據(jù)命令處理。這里的命令實(shí)際上就是按鈕文本。

      public String process(String command) {

      for (String pattern : processors.keySet()) {

      if (command.matches(pattern)) {

      processors.get(pattern).calculate(command);

      break;

      }

      }

      return displayText;

      }

      // 當(dāng)按下 CE 時(shí)

      private void clearErrorClicked() {

      if (operator == null) {

      number1 = null;

      } else {

      number2 = null;

      }

      displayText = "0";

      reset = true;

      }

      // 當(dāng)按下 C 時(shí),將計(jì)算器置為初始狀態(tài)。

      private void clearClicked() {

      number1 = null;

      number2 = null;

      operator = null;

      displayText = "0";

      reset = true;

      }

      // 當(dāng)按下 = 時(shí)

      private void equalsClicked() {

      calculateResult();

      number1 = null;

      number2 = null;

      operator = null;

      reset = true;

      }

      // 計(jì)算結(jié)果

      private void calculateResult() {

      number2 = new BigDecimal(displayText);

      Operator oper = operators.get(operator);

      if (oper != null) {

      BigDecimal result = oper.process(number1, number2);

      displayText = result.toString();

      }

      }

      // 當(dāng)按下 +-*/ 時(shí)(這里也可以擴(kuò)展成其他中間操作符)

      private void operatorClicked(String command) {

      if (operator != null) {

      calculateResult();

      }

      number1 = new BigDecimal(displayText);

      operator = command;

      reset = true;

      }

      // 當(dāng)按下 . 時(shí)

      private void dotClicked() {

      if (displayText.indexOf(".") == -1) {

      displayText += ".";

      } else if (reset) {

      displayText = "0.";

      }

      reset = false;

      }

      // 當(dāng)按下 0-9 時(shí)

      private void numberClicked(String command) {

      if (reset) {

      displayText = command;

      } else {

      displayText += command;

      }

      reset = false;

      }

      // 運(yùn)算符處理接口

      interface Operator {

      BigDecimal process(BigDecimal number1, BigDecimal number2);

      }

      // 按鈕處理接口

      interface Processor {

      void calculate(String command);

      }

      }

      計(jì)算器java代碼

      import java.awt.Color;

      import java.awt.Font;

      import java.awt.GridLayout;

      import java.awt.event.ActionEvent;

      import java.awt.event.ActionListener;

      import javax.swing.JButton;

      import javax.swing.JFrame;

      import javax.swing.JLabel;

      import javax.swing.JPanel;

      import javax.swing.JTextField;

      import javax.swing.WindowConstants;

      import javax.swing.border.LineBorder;

      class Normal{

      double i,j;

      public Normal(double num1,double num2){

      i=num1;

      j=num2;

      }

      public double puls(){

      return i+j;

      }

      public double subtract(){

      return i-j;

      }

      public double multiply(){

      return i*j;

      }

      public double divide(){

      return i/j;

      }

      public double surpuls(){

      return i%j;

      }

      }

      class scientific extends Normal{

      public scientific(int num1, int num2) {

      super(num1, num2);

      }

      }

      public class calc extends JFrame{

      public static void main(String[] args) {

      viewNormal VN= new viewNormal("normal");

      }

      }

      class viewNormal extends JFrame implements ActionListener{

      JPanel jp1 = new JPanel(new GridLayout(4,3,5,5));

      JPanel jp2 = new JPanel(new GridLayout(5,1,5,5));

      JLabel jl;

      JButton[] jb;

      JButton jbs,jbo,jba,jbb,jbc,jby;

      StringBuffer sb = new StringBuffer();

      Normal normal;

      int dot=0;

      double fnum=0;

      double lnum=0;

      double result;

      String sign=null;

      public viewNormal(String title){

      setTitle(title);

      setLayout(null);

      setVisible(true);

      setBounds(200,200,305,350);

      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

      jb= new JButton[12];

      for(int i=0;i9;i++){

      jb[i]=new JButton(""+(i+1));

      jp1.add(jb[i]);

      jb[i].addActionListener(this);

      }

      jb[9]=new JButton(".");

      jb[10]=new JButton("0");

      jb[11]=new JButton("=");

      jb[9].addActionListener(this);

      jb[10].addActionListener(this);

      jb[11].addActionListener(this);

      jp1.add(jb[9]);

      jp1.add(jb[10]);

      jp1.add(jb[11]);

      jp1.setBounds(10, 100, 200, 200);

      jbs= new JButton("+");jbo= new JButton("-");jba= new JButton("*");

      jbb= new JButton("/");jby= new JButton("%");jbc= new JButton("C");

      jbs.addActionListener(this);jbo.addActionListener(this);jba.addActionListener(this);

      jbb.addActionListener(this);jby.addActionListener(this);jbc.addActionListener(this);

      //jp2.add(jby);

      jp2.add(jbs);jp2.add(jbo);jp2.add(jba);jp2.add(jbb);jp2.add(jbc);

      jp2.setBounds(215, 100, 70, 200);

      jl= new JLabel("0",JLabel.RIGHT);

      jl.setFont(new Font("Batang",Font.BOLD, 20));

      jl.setBorder(new LineBorder(Color.black,2));

      jl.setBackground(Color.white);

      jl.setBounds(10, 40, 275, 50);

      jl.setOpaque(true);

      add(jl);

      add(jp1);

      add(jp2);

      }

      //+

      public void sum(){

      lnum=Double.parseDouble(sb.toString());

      normal=new Normal(fnum,lnum);

      fnum=normal.puls();

      result=fnum;

      }

      //-

      private void sub() {

      System.out.println(sb.toString());

      lnum=Double.parseDouble(sb.toString());

      normal=new Normal(fnum,lnum);

      fnum=normal.subtract();

      result=fnum;

      }

      //*

      private void mul() {

      lnum=Double.parseDouble(sb.toString());

      normal=new Normal(fnum,lnum);

      fnum=normal.multiply();

      result=fnum;

      }

      // /

      private void div() {

      lnum=Double.parseDouble(sb.toString());

      normal=new Normal(fnum,lnum);

      fnum=normal.divide();

      result=fnum;

      }

      //%

      private void sur() {

      lnum=Double.parseDouble(sb.toString());

      normal=new Normal(fnum,lnum);

      fnum=normal.surpuls();

      result=fnum;

      }

      // =

      private void same(){

      if(sign.equals("+")){

      sum();

      }

      if(sign.equals("-")){

      sub();

      }

      if(sign.equals("*")){

      mul();

      }

      if(sign.equals("/")){

      div();

      }

      if(sign.equals("%")){

      sur();

      }

      }

      //result

      public void Result(){

      if(result%1!=0)

      jl.setText(""+result);

      else

      {

      int i=(int)result;

      jl.setText(""+i);

      }

      }

      @Override

      public void actionPerformed(ActionEvent e) {

      //System.out.println(sb.toString());

      // 1~9

      for(int i=0;i9;i++){

      if(e.getSource()==jb[i]!sb.toString().equals("0")){

      sb.append(jb[i].getText());

      jl.setText(sb.toString());

      }

      else if(e.getSource()==jb[i]sb.toString().equals("0")){

      int d=sb.length();

      sb.delete(0, d);

      sb.append(jb[i].getText());

      jl.setText(sb.toString());

      }

      }

      // 0

      if(e.getSource()==jb[10]!sb.toString().equals("0")){

      sb.append(jb[10].getText());

      jl.setText(sb.toString());

      }

      // .

      if(e.getSource()==jb[9]dot==0!sb.toString().equals("")){

      dot++;

      sb.append(jb[9].getText());

      jl.setText(sb.toString());

      }

      // =

      if(e.getSource()==jb[11]!sb.toString().equals("")){

      same();

      Result();

      int d=sb.length();

      sb.delete(0, d);

      dot=0;

      }

      // +

      if(e.getSource()==jbs!sb.toString().equals("")){

      if(sign!="+"sign!=null)

      same();

      else

      sum();

      sign ="+";

      Result();

      int d=sb.length();

      sb.delete(0, d);

      dot=0;

      }

      //-

      if(e.getSource()==jbo!sb.toString().equals("")){

      if(fnum==0)

      fnum=2*Double.parseDouble(sb.toString());

      if(sign!="-"sign!=null)

      same();

      else

      sub();

      sign ="-";

      Result();

      int d=sb.length();

      sb.delete(0, d);

      dot=0;

      }

      //*

      if(e.getSource()==jba!sb.toString().equals("")){

      if(fnum==0)

      fnum=1;

      if(sign!="*"sign!=null)

      same();

      else

      mul();

      sign ="*";

      Result();

      int d=sb.length();

      sb.delete(0, d);

      dot=0;

      }

      // /

      if(e.getSource()==jbb!sb.toString().equals("")){

      if(fnum==0)

      fnum=Math.pow(Double.parseDouble(sb.toString()),2);

      if(sign!="/"sign!=null)

      same();

      else

      div();

      sign ="/";

      Result();

      int d=sb.length();

      sb.delete(0, d);

      dot=0;

      }

      //%

      // if(e.getSource()==jby!sb.toString().equals("")){

      // if(fnum==0){

      // fnum=Double.parseDouble(sb.toString());

      // result=fnum;

      // }

      // else {

      // if(sign!="%"sign!=null)

      // same();

      // else{

      // lnum=Double.parseDouble(sb.toString());

      // normal=new Normal(fnum,lnum);

      // fnum=normal.surpuls();

      // result=fnum;

      // }

      // }

      // sign ="%";

      // Result();

      // int d=sb.length();

      // sb.delete(0, d);

      // dot=0;

      // }

      //clear

      if(e.getSource()==jbc){

      int d=sb.length();

      sb.delete(0, d);

      jl.setText("0");

      dot=0;

      fnum=0;

      lnum=0;

      sign=null;

      }

      }

      }

      class viewScientific extends viewNormal{

      public viewScientific(String title){

      super(title);

      setBounds(200,200,800,500);

      }

      }

      //等號(hào)以后輸入符號(hào)用不了, String轉(zhuǎn) double 本來(lái)就有錯(cuò)誤,你可以用我的擴(kuò)展成科學(xué)型的。

      求用JAVA實(shí)現(xiàn)計(jì)算器的代碼(可實(shí)用的,沒(méi)語(yǔ)法錯(cuò)誤的)

      import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.TitledBorder; //導(dǎo)包 public class Jisuanqi extends JFrame implements ActionListener { //繼承JFrame 實(shí)現(xiàn)事件監(jiān)聽(tīng) private JTextField reasult; private JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnAC, btnAdd, btnSub, btnReasult, btnD, btnAbout, btnCancel; private boolean add, sub, end, s, c; private String str; private double num1, num2; public Jisuanqi() { //構(gòu)造屬性 JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); TitledBorder tb = new TitledBorder("輸出"); tb.setTitleColor(Color.BLUE); //標(biāo)題邊框底端線 設(shè)置顏色 btnAbout = new JButton(" 關(guān)于 "); btnCancel = new JButton("Cancel"); //兩個(gè)按鈕 btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { System.exit(0); } }); //給Cancel添加事件監(jiān)聽(tīng) 當(dāng)鼠標(biāo)點(diǎn)擊時(shí) 程序結(jié)束 btnAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { JOptionPane.showMessageDialog(null, "黃蓋!!", "消息", JOptionPane.INFORMATION_MESSAGE); } //給“關(guān)于”添加事件監(jiān)聽(tīng) 當(dāng)鼠標(biāo)點(diǎn)擊時(shí) 彈出對(duì)話框 顯示黃蓋 }); p3.add(btnAbout); p3.add(btnCancel); // JPanel p4=new JPanel(); // JPanel p5=new JPanel(); // reasult.setBorder(tb); reasult = new JTextField("0", 20); reasult.setEditable(false); //設(shè)置不能修改 reasult.setHorizontalAlignment(JTextField.RIGHT); // 設(shè)置文本的水平對(duì)齊方式。 reasult.setForeground(Color.BLUE); //顏色 p1.setBorder(tb); p1.add(reasult); btn0 = new JButton("0"); btn0.addActionListener(this); btn1 = new JButton("1"); btn1.addActionListener(this); btn2 = new JButton("2"); btn2.addActionListener(this); btn3 = new JButton("3"); btn3.addActionListener(this); btn4 = new JButton("4"); btn4.addActionListener(this); btn5 = new JButton("5"); btn5.addActionListener(this); btn6 = new JButton("6"); btn6.addActionListener(this); btn7 = new JButton("7"); btn7.addActionListener(this); btn8 = new JButton("8"); btn8.addActionListener(this); btn9 = new JButton("9"); btn9.addActionListener(this); btnD = new JButton("."); btnD.addActionListener(this); btnD.setForeground(Color.RED); btnAC = new JButton("AC"); btnAC.addActionListener(this); btnAC.setBackground(Color.PINK); btnAdd = new JButton("+"); btnAdd.addActionListener(this); btnAdd.setForeground(Color.BLUE); btnSub = new JButton("—"); btnSub.addActionListener(this); btnSub.setForeground(Color.BLUE); btnReasult = new JButton("="); btnReasult.addActionListener(this); btnReasult.setForeground(Color.RED); //事件監(jiān)聽(tīng) + 顏色 p2.add(btn1); p2.add(btn2); p2.add(btn3); p2.add(btn4); p2.add(btn5); p2.add(btn6); p2.add(btn7); p2.add(btn8); p2.add(btn9); p2.add(btn0); p2.add(btnD); p2.add(btnAC); p2.add(btnAdd); p2.add(btnSub); p2.add(btnReasult); //面板上添加按鈕 p2.setLayout(new GridLayout(5, 3)); //面板上設(shè)置對(duì)齊方式 add(p1, BorderLayout.NORTH); add(p2, BorderLayout.CENTER); add(p3, BorderLayout.SOUTH); //將p1 p2 p3 面板對(duì)象添加到JFrame } public void num(int i) { String s = null; s = String.valueOf(i); if (end) { // 如果數(shù)字輸入結(jié)束,則將文本框置零,重新輸入 reasult.setText("0"); end = false; } if ((reasult.getText()).equals("0")) { // 如果文本框的內(nèi)容為零,則覆蓋文本框的內(nèi)容 reasult.setText(s); } else { // 如果文本框的內(nèi)容不為零,則在內(nèi)容后面添加數(shù)字 str = reasult.getText() + s; reasult.setText(str); } }/* * * String s=null; * * s=String.valueOf(i); * * str=reasult.getText()+s; * * reasult.setText(str); * * } */ public void actionPerformed(ActionEvent e) { if (e.getSource() == btn1) num(1); else if (e.getSource() == btn2) num(2); else if (e.getSource() == btn3) num(3); else if (e.getSource() == btn4) num(4); else if (e.getSource() == btn5) num(5); else if (e.getSource() == btn6) num(6); else if (e.getSource() == btn7) num(7); else if (e.getSource() == btn8) num(8); else if (e.getSource() == btn9) num(9); else if (e.getSource() == btn0) num(0); else if (e.getSource() == btnAdd) { sign(1); btnD.setEnabled(true); } else if (e.getSource() == btnSub) { sign(2); btnD.setEnabled(true); } else if (e.getSource() == btnAC) { btnD.setEnabled(true); reasult.setText("0"); } else if (e.getSource() == btnD) { str = reasult.getText(); str += "."; reasult.setText(str); btnD.setEnabled(false); } else if (e.getSource() == btnReasult) { btnD.setEnabled(true); num2 = Double.parseDouble(reasult.getText()); if (add) { num1 = num1 + num2; } else if (sub) { num1 = num1 - num2; } reasult.setText(String.valueOf(num1)); end = true; } } public void sign(int s) { if (s == 1) { add = true; sub = false; } else if (s == 2) { add = false; sub = true; } num1 = Double.parseDouble(reasult.getText()); end = true; } //設(shè)計(jì)計(jì)算的過(guò)程 public static void main(String[] args) { Jisuanqi j = new Jisuanqi(); j.setTitle("+/-簡(jiǎn)易計(jì)算器"); j.setLocation(500, 280); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默認(rèn)關(guān)閉可以關(guān)閉程序 j.setResizable(false); j.pack(); j.setVisible(true); } } 這個(gè)計(jì)算機(jī),絕對(duì)讓你滿意

      JAVA 編寫計(jì)算器 要代碼最簡(jiǎn)單的

      學(xué)java的時(shí)候自己編的,很簡(jiǎn)單,能夠連續(xù)輸入計(jì)算式后進(jìn)行計(jì)算

      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;

      /**簡(jiǎn)易計(jì)算器,能夠進(jìn)行簡(jiǎn)單的計(jì)算

      *

      * @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="";//保存結(jié)果

      StringBuilder sb = new StringBuilder();//保存要進(jìn)行的計(jì)算式

      int x = 0; //用來(lái)判斷上一次的事件類型

      String str = "123+456-789*0.=/";

      ArrayListString arrayList = new ArrayListString();//保存計(jì)算式,通過(guò)方法進(jìn)行運(yùn)算

      public CalculatorA(){

      frame = new JFrame("我的計(jì)算器v1.1");

      frame.setLocation(300,300);

      field = new JTextField(25);

      allButtons = new JButton[16];

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

      allButtons[i]= new JButton(str.substring(i,i+1));

      }

      clearButton = new JButton("CLEAR");

      // backButton = new JButton("——");

      init();

      setFondAndColor();

      addEventHander();

      }

      public void init(){

      frame.setLayout(new BorderLayout());

      JPanel northPanel = new JPanel();

      JPanel centerPanel = new JPanel();

      JPanel southPanel = new JPanel();

      northPanel.setLayout(new FlowLayout());

      centerPanel.setLayout(new GridLayout(4,4));

      southPanel.setLayout(new FlowLayout());

      northPanel.add(field);

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

      centerPanel.add(allButtons[i]);

      }

      southPanel.add(clearButton);

      //southPanel.add(backButton);

      frame.add(northPanel,BorderLayout.NORTH);

      frame.add(centerPanel,BorderLayout.CENTER);

      frame.add(southPanel,BorderLayout.SOUTH);

      }

      //設(shè)置輸入字體

      public void setFondAndColor(){

      field.setFont(new Font("宋體",Font.BOLD,24));

      field.setBackground(new Color(100,200,200));

      field.setForeground(Color.RED);

      //設(shè)置字體從右起始

      field.setHorizontalAlignment(JTextField.RIGHT);

      }

      public void showMi(){

      frame.pack();

      frame.setResizable(false);

      frame.setVisible(true);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      }

      public void addEventHander(){

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

      allButtons[i].addActionListener(this);

      }

      clearButton.addActionListener(this);

      // backButton.addActionListener(this);

      }

      @Override

      public void actionPerformed(ActionEvent e) {

      String str = e.getActionCommand();//取得當(dāng)前事件返回的值

      if("0123456789.".indexOf(str)!=-1){

      if(x == 0){ //當(dāng)x為0時(shí)表示還沒(méi)有進(jìn)行輸入

      result=str;

      sb.append(str);

      field.setText(str);

      x = 1;

      }

      else if(x == 1){

      result = result +str;

      sb.append(str);

      field.setText(result);

      x = 1;

      }

      else if(x == 2){

      sb.delete(0,sb.length());

      result = result+str;

      sb.append(str);

      field.setText(result);

      x = 1;

      }

      else if(x == 3){

      result = str;

      sb.delete(0,sb.length());

      arrayList.clear();

      field.setText(str);

      sb.append(str);

      field.setText(str);

      x = 1;

      }

      else if(x == 4){

      result ="";

      sb.delete(0,sb.length());

      arrayList.clear();

      result = str;

      sb.append(str);

      field.setText(str);

      x = 1;

      }

      else{

      result = result +str;

      sb.append(str);

      field.setText(result);

      x = 1;

      }

      }

      else if("+*-/".indexOf(str)!=-1){

      if(x == 0){

      field.setText("");

      x = 2;

      }

      else if(x == 1){

      result = result + str;

      arrayList.add(sb.toString());

      arrayList.add(str);

      sb.append(str);

      field.setText(result);

      x = 2;

      }

      else if(x == 2){

      x = 2;

      }

      else if(x == 3){

      field.setText(result+str);

      arrayList.add(result);

      arrayList.add(str);

      result = result+str;

      x = 2;

      }

      else if(x == 4){

      result ="";

      sb.delete(0,sb.length());

      arrayList.clear();

      x = 2;

      }

      else{

      field.setText(result+str);

      arrayList.add(result);

      arrayList.add(str);

      result = result+str;

      x = 2;

      }

      }

      else if("=".equals(str)){

      if(x == 0){

      field.setText("0");

      arrayList.clear();

      result = "0";

      x = 3;

      }

      else if(x == 1){

      try{

      arrayList.add(sb.toString());

      arrayList = getResult(arrayList);

      result = arrayList.get(0);

      field.setText(result);

      arrayList.clear();

      x = 3;

      }catch(Exception e1){

      field.setText("數(shù)據(jù)格式異常");

      x = 0;

      }

      }

      else if(x == 2){

      field.setText("數(shù)據(jù)格式錯(cuò)誤.....");

      arrayList.clear();

      x = 0;

      }

      else if(x == 3){

      field.setText(result);

      x = 3;

      }

      else if(x == 4){

      result ="";

      sb.delete(0,sb.length());

      arrayList.clear();

      x = 3;

      }

      else {

      try{

      arrayList.add(sb.toString());

      arrayList = getResult(arrayList);

      result = arrayList.get(0);

      field.setText(result);

      arrayList.clear();

      x = 3;

      }catch(Exception e1){

      field.setText("數(shù)據(jù)格式異常");

      x = 0;

      }

      }

      }

      else if("CLEAR".equals(str)){

      arrayList.clear();

      field.setText("0");

      arrayList.add("0");

      x = 4;

      }

      else{

      if(result.length()1){

      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 ArrayListString getResult(ArrayListString list){

      String res = null;

      String[] s = {"/","*","-","+"};

      int i=0;

      if(list.size()1){

      for(;is.length;){

      if(s[i].equals("/")){

      for(int j=0;jlist.size();j++){

      if(list.get(j).equals(s[i])){

      res = Double.toString(Double.parseDouble(list.get(j-1))/Double.parseDouble(list.get(j+1)));

      //本地的數(shù)據(jù)格式

      NumberFormat nf = NumberFormat.getInstance();

      res = nf.format(Double.parseDouble(res));

      res = getChange(res);

      list.set(j-1,res);

      list.remove(j);

      list.remove(j);

      getResult(list);

      }

      }

      i++;

      }

      else if(s[i].equals("*")){

      for(int j=0;jlist.size();j++){

      if(list.get(j).equals(s[i])){

      res = Double.toString(Double.parseDouble(list.get(j-1))*Double.parseDouble(list.get(j+1)));

      NumberFormat nf = NumberFormat.getInstance();

      res = nf.format(Double.parseDouble(res));

      res = getChange(res);

      list.set(j-1,res);

      list.remove(j);

      list.remove(j);

      getResult(list);

      }

      }

      i++;

      }

      else if(s[i].equals("-")){

      for(int j=0;jlist.size();j++){

      if(list.get(j).equals(s[i])){

      res = Double.toString(Double.parseDouble(list.get(j-1))-Double.parseDouble(list.get(j+1)));

      NumberFormat nf = NumberFormat.getNumberInstance();

      res = nf.format(Double.parseDouble(res));

      res = getChange(res);

      list.set(j-1,res);

      list.remove(j);

      list.remove(j);

      getResult(list);

      }

      }

      i++;

      }

      else {

      for(int j=0;jlist.size();j++){

      if(list.get(j).equals(s[i])){

      res = Double.toString(Double.parseDouble(list.get(j-1))+Double.parseDouble(list.get(j+1)));

      NumberFormat nf = NumberFormat.getInstance();

      res = nf.format(Double.parseDouble(res));

      res = getChange(res);

      list.set(j-1,res);

      list.remove(j);

      list.remove(j);

      getResult(list);

      }

      }

      i++;

      }

      }

      }

      return list;

      }

      //對(duì)數(shù)字字符串進(jìn)行排除不必要符號(hào)

      public static String getChange(String res){

      String s_temp = "";

      char[] c = new char[res.length()];

      for(int k=0;kc.length;k++){

      c[k] = res.charAt(k);

      }

      for(int k=0;kc.length;k++){

      if((c[k]= '0' c[k]= '9')|| c[k] == '.'){

      s_temp += c[k];

      }

      }

      return s_temp;

      }

      public static void main(String[] args){

      new CalculatorA().showMi();

      }

      }


      網(wǎng)頁(yè)名稱:java命令行計(jì)算器代碼,java實(shí)現(xiàn)計(jì)算器代碼
      標(biāo)題網(wǎng)址:http://ef60e0e.cn/article/hsicgd.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        萍乡市| 梅州市| 卢龙县| 南陵县| 宿迁市| 孟津县| 怀宁县| 敦煌市| 驻马店市| 临泽县| 宁都县| 司法| 若尔盖县| 泸西县| 文安县| 德惠市| 上饶市| 龙井市| 夏邑县| 永春县| 郎溪县| 武陟县| 巨鹿县| 平度市| 嘉义市| 饶阳县| 台前县| 临武县| 英山县| 临邑县| 城固县| 沈阳市| 满洲里市| 镇坪县| 大洼县| 宜城市| 平和县| 阿图什市| 民丰县| 宁津县| 鹤山市|