微信小程序 > 微信公开课 > java:编写一个计算器小程序,要求可以做加减乘除运算
微信小程序,微信公开课,java:编写一个计算器小程序,要求可以做加减乘除运算

java:编写一个计算器小程序,要求可以做加减乘除运算

2019-03-12 17:01:43

微信公开课小程序热门讨论「java:编写一个计算器小程序,要求可以做加减乘除运算###kankan我的importjava.awt.BorderLayout;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.math.BigDecimal;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JTextField;publicclassTestCalcimplementsActionListener{JFramejf=newJFrame(计算器);JPaneljp=newJPanel();JTextFieldjtf=newJTextField(0.,200);JButton[]jb=newJButton[20];privateinttag=0;privatedoublea;privatedoubleb;privateStringoperator;StringBuildersb1=newStringBuilder();StringBuildersb2=newStringBuilder();publicTestCalc(){jf.add(jtf,BorderLayout.NORTH);//jtf.requestFocus();jtf.setEditable(false);jtf.setCaretPosition(jtf.getText().length()1);jf.add(jp);jp.setLayout(newGridLayout(5,4,4,4));String[]str={Back,CE,C,+,7,8,9,,4,5,6,*,1,2,3,/,0,+/,.,=};inti=0;for(i=0;istr.length;i++){jb[i]=newJButton(str[i]);jp.add(jb[i]);jb[i].addActionListener(this);}jf.setSize(300,240);//jf.setLocation(300,200);jf.setLocationRelativeTo(null);jf.setVisible(true);//jf.pack();//自动调整大小;jf.setResizable(false);//不允许别人调大小;jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicstaticvoidmain(String[]args){TestCalctc=newTestCalc();}publicvoidactionPerformed(ActionEvente){Stringcommand=e.getActionCommand();if(command.matches(^[[09].]$)){if(tag==0){sb1.append(command);jtf.setText(sb1.substring(0));}else{sb2.append(command);jtf.setText(sb2.substring(0));}}elseif(command.matches(^[/*+]$)){tag=1;operator=command;}else{if(command.matches(=)){Stringstr1=sb1.substring(0);Stringstr2=sb2.substring(0);if(str1==null){a=0.0;}else{a=Double.parseDouble(str1);}if(str2==null){b=0.0;}else{b=Double.parseDouble(str2);}if(operator.equals(+)){jtf.setText(+(a+b));}elseif(operator.equals()){BigDecimalbd1=newBigDecimal(Double.toString(a));//必须使用String做参数才可以精确运算BigDecimalbd2=newBigDecimal(Double.toString(b));Doubleyu1=bd1.subtract(bd2).doubleValue();jtf.setText(+(yu1));}elseif(operator.equals(*)){jtf.setText(+(a*b));}elseif(operator.equals(/)){BigDecimalbd1=newBigDecimal(Double.toString(a));//必须使用String做参数才可以精确运算BigDecimalbd2=newBigDecimal(Double.toString(b));Doubleyu1=bd1.divide(bd2).doubleValue();jtf.setText(+yu1);}tag=0;sb1.delete(0,sb1.length());sb2.delete(0,sb2.length());}elseif(command.matches(C)){tag=0;sb1.delete(0,sb1.length());sb2.delete(0,sb2.length());jtf.setText(0.);jtf.setCaretPosition(jtf.getText().length()1);}elseif(command.matches(CE)){tag=0;sb2.delete(0,sb2.length());jtf.setText(sb1.substring(0));}elseif(command.matches(Back)){//Back功能键的实现;if(tag==0){sb1.deleteCharAt(sb1.length()1);jtf.setText(sb1.substring(0));}else{sb2.deleteCharAt(sb2.length()1);jtf.setText(sb2.substring(0));}}elseif(command.matches(\\+/)){if(tag==0){if(sb1.substring(0,1).equals()){sb1.replace(0,1,+);}else{sb1.insert(0,);}jtf.setText(sb1.substring(0));}else{if(sb2.substring(0,1).equals()){sb2.replace(0,1,+);}else{sb2.insert(0,);}jtf.setText(sb2.substring(0));}}else{}}}}
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassCalculatorextendsJFrameimplementsActionListener{privatestaticfinallongserialVersionUID=8199443193151152362L;privateJButtonbto_s=newJButton(sqrt),bto_zf=newJButton(+/-),bto_ce=newJButton(CE),bto_c=newJButton(C),bto_7=newJButton(7),bto_8=newJButton(8),bto_9=newJButton(9),bto_chu=newJButton(/),bto_4=newJButton(4),bto_5=newJButton(5),bto_6=newJButton(6),bto_cheng=newJButton(*),bto_1=newJButton(1),bto_2=newJButton(2),bto_3=newJButton(3),bto_jian=newJButton(-),bto_0=newJButton(0),bto_dian=newJButton(.),bto_deng=newJButton(=),bto_jia=newJButton(+);JButtonbutton[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,bto_0,bto_dian,bto_deng,bto_jia};privateJTextFieldtext_double;//=newJTextField(0);privateStringoperator==;//当前运算的运算符privatebooleanfirstDigit=true;//标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字privatedoubleresultNum=0.0;//计算的中间结果privatebooleanoperateValidFlag=true;//判断操作是否合法publicCalculator(){  super(Calculator);  this.setBounds(300,300,300,300);  this.setResizable(false);  this.setBackground(Color.orange);  this.setDefaultCloseOperation(EXIT_ON_CLOSE);  this.getContentPane().setLayout(newBorderLayout());//设置布局  text_double=newJTextField(0,20);//设置文本区  text_double.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式未右对齐  this.getContentPane().add(text_double,BorderLayout.NORTH);//将文本区添加到Content北部  JPanelpanel=newJPanel(newGridLayout(5,4));//在内容窗口添加一个网格布局  this.getContentPane().add(panel);//添加panel面板  for(inti=0;ibutton.length;i++)//在面板上添加按钮panel.add(button[i]);for(inti=0;ibutton.length;i++)  button[i].addActionListener(this);//为按钮注册  text_double.setEditable(false);//文本框不可编辑  text_double.addActionListener(this);//this.setVisible(true);}publicvoidactionPerformed(ActionEvente)//{  Stringc=e.getActionCommand();//返回与此动作相关的命令字符串。  System.out.println(#######」最新回复-commandis+c);  if(c.equals(C)){handleC();//用户按了“C”键  }  elseif(c.equals(CE))//用户按了CE键  {text_double.setText(0);  }  elseif(0123456789..indexOf(c)=0)//用户按了数字键或者小数点键  {handleNumber(c);//handlezero(zero);  }else //用户按了运算符键  {handleOperator(c);  }}privatevoidhandleC()//初始化计算器的各种值{  text_double.setText(0);  firstDigit=true;  operator==;  }privatevoidhandleNumber(Stringbutton){  if(firstDigit)//输入的第一个数字  {  text_double.setText(button);  }elseif((button.equals(.))&&(text_double.getText().indexOf(.)0))//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面//如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回-1  {  text_double.setText(text_double.getText()+.);  }elseif(!button.equals(.))//  如果输入的不是小数点,则将数字附在结果文本框的后面  {  text_double.setText(text_double.getText()+button);  }//  以后输入的肯定不是第一个数字了  firstDigit=false;  }privatevoidhandleOperator(Stringbutton){if(operator.equals(/)){//  除法运算//  如果当前结果文本框中的值等于0  if(getNumberFromText()==0.0){//  操作不合法  operateValidFlag=false;  text_double.setText(除数不能为零);  }else{  resultNum/=getNumberFromText();  }  }elseif(operator.equals(+)){//  加法运算  resultNum+=getNumberFromText();  }elseif(operator.equals(-)){//  减法运算  resultNum-=getNumberFromText();  }elseif(operator.equals(*)){//  乘法运算  resultNum*=getNumberFromText();  }elseif(operator.equals(sqrt)){//  平方根运算if(getNumberFromText()0){ operateValidFlag=false;text_double.setText(被开方数不能为负数);}else  resultNum=Math.sqrt(resultNum);  }  elseif(operator.equals(+/-)){//正数负数运算resultNum=resultNum*(-1);}elseif(operator.equals(=)){//赋值运算resultNum=getNumberFromText();}  if(operateValidFlag){//双精度浮点数的运算longt1;doublet2;t1=(long)resultNum;t2=resultNum-t1;if(t2==0){text_double.setText(String.valueOf(t1));}else{text_double.setText(String.valueOf(resultNum));}}  operator=button;//运算符等于用户按的按钮  firstDigit=true;  operateValidFlag=true;}privatedoublegetNumberFromText()//从结果的文本框获取数字{  doubleresult=0;  try{  result=Double.valueOf(text_double.getText()).doubleValue();//ValueOf()返回表示指定的double值的Double实例  }catch(NumberFormatExceptione){  }  returnresult;  }publicstaticvoidmain(finalString[]args){newCalculator();}} 更多有关「java:编写一个计算器小程序,要求可以做加减乘除运算###kankan我的importjava.awt.BorderLayout;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.math.BigDecimal;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JTextField;publicclassTestCalcimplementsActionListener{JFramejf=newJFrame(计算器);JPaneljp=newJPanel();JTextFieldjtf=newJTextField(0.,200);JButton[]jb=newJButton[20];privateinttag=0;privatedoublea;privatedoubleb;privateStringoperator;StringBuildersb1=newStringBuilder();StringBuildersb2=newStringBuilder();publicTestCalc(){jf.add(jtf,BorderLayout.NORTH);//jtf.requestFocus();jtf.setEditable(false);jtf.setCaretPosition(jtf.getText().length()1);jf.add(jp);jp.setLayout(newGridLayout(5,4,4,4));String[]str={Back,CE,C,+,7,8,9,,4,5,6,*,1,2,3,/,0,+/,.,=};inti=0;for(i=0;istr.length;i++){jb[i]=newJButton(str[i]);jp.add(jb[i]);jb[i].addActionListener(this);}jf.setSize(300,240);//jf.setLocation(300,200);jf.setLocationRelativeTo(null);jf.setVisible(true);//jf.pack();//自动调整大小;jf.setResizable(false);//不允许别人调大小;jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicstaticvoidmain(String[]args){TestCalctc=newTestCalc();}publicvoidactionPerformed(ActionEvente){Stringcommand=e.getActionCommand();if(command.matches(^[[09].]$)){if(tag==0){sb1.append(command);jtf.setText(sb1.substring(0));}else{sb2.append(command);jtf.setText(sb2.substring(0));}}elseif(command.matches(^[/*+]$)){tag=1;operator=command;}else{if(command.matches(=)){Stringstr1=sb1.substring(0);Stringstr2=sb2.substring(0);if(str1==null){a=0.0;}else{a=Double.parseDouble(str1);}if(str2==null){b=0.0;}else{b=Double.parseDouble(str2);}if(operator.equals(+)){jtf.setText(+(a+b));}elseif(operator.equals()){BigDecimalbd1=newBigDecimal(Double.toString(a));//必须使用String做参数才可以精确运算BigDecimalbd2=newBigDecimal(Double.toString(b));Doubleyu1=bd1.subtract(bd2).doubleValue();jtf.setText(+(yu1));}elseif(operator.equals(*)){jtf.setText(+(a*b));}elseif(operator.equals(/)){BigDecimalbd1=newBigDecimal(Double.toString(a));//必须使用String做参数才可以精确运算BigDecimalbd2=newBigDecimal(Double.toString(b));Doubleyu1=bd1.divide(bd2).doubleValue();jtf.setText(+yu1);}tag=0;sb1.delete(0,sb1.length());sb2.delete(0,sb2.length());}elseif(command.matches(C)){tag=0;sb1.delete(0,sb1.length());sb2.delete(0,sb2.length());jtf.setText(0.);jtf.setCaretPosition(jtf.getText().length()1);}elseif(command.matches(CE)){tag=0;sb2.delete(0,sb2.length());jtf.setText(sb1.substring(0));}elseif(command.matches(Back)){//Back功能键的实现;if(tag==0){sb1.deleteCharAt(sb1.length()1);jtf.setText(sb1.substring(0));}else{sb2.deleteCharAt(sb2.length()1);jtf.setText(sb2.substring(0));}}elseif(command.matches(\\+/)){if(tag==0){if(sb1.substring(0,1).equals()){sb1.replace(0,1,+);}else{sb1.insert(0,);}jtf.setText(sb1.substring(0));}else{if(sb2.substring(0,1).equals()){sb2.replace(0,1,+);}else{sb2.insert(0,);}jtf.setText(sb2.substring(0));}}else{}}}}
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassCalculatorextendsJFrameimplementsActionListener{privatestaticfinallongserialVersionUID=8199443193151152362L;privateJButtonbto_s=newJButton(sqrt),bto_zf=newJButton(+/-),bto_ce=newJButton(CE),bto_c=newJButton(C),bto_7=newJButton(7),bto_8=newJButton(8),bto_9=newJButton(9),bto_chu=newJButton(/),bto_4=newJButton(4),bto_5=newJButton(5),bto_6=newJButton(6),bto_cheng=newJButton(*),bto_1=newJButton(1),bto_2=newJButton(2),bto_3=newJButton(3),bto_jian=newJButton(-),bto_0=newJButton(0),bto_dian=newJButton(.),bto_deng=newJButton(=),bto_jia=newJButton(+);JButtonbutton[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,bto_0,bto_dian,bto_deng,bto_jia};privateJTextFieldtext_double;//=newJTextField(0);privateStringoperator==;//当前运算的运算符privatebooleanfirstDigit=true;//标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字privatedoubleresultNum=0.0;//计算的中间结果privatebooleanoperateValidFlag=true;//判断操作是否合法publicCalculator(){  super(Calculator);  this.setBounds(300,300,300,300);  this.setResizable(false);  this.setBackground(Color.orange);  this.setDefaultCloseOperation(EXIT_ON_CLOSE);  this.getContentPane().setLayout(newBorderLayout());//设置布局  text_double=newJTextField(0,20);//设置文本区  text_double.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式未右对齐  this.getContentPane().add(text_double,BorderLayout.NORTH);//将文本区添加到Content北部  JPanelpanel=newJPanel(newGridLayout(5,4));//在内容窗口添加一个网格布局  this.getContentPane().add(panel);//添加panel面板  for(inti=0;ibutton.length;i++)//在面板上添加按钮panel.add(button[i]);for(inti=0;ibutton.length;i++)  button[i].addActionListener(this);//为按钮注册  text_double.setEditable(false);//文本框不可编辑  text_double.addActionListener(this);//this.setVisible(true);}publicvoidactionPerformed(ActionEvente)//{  Stringc=e.getActionCommand();//返回与此动作相关的命令字符串。  System.out.println(#######」的疑问请扫码关注微信公开课+小程序!
标签:微信公开课,小程序
微信小程序,微信公开课,java:编写一个计算器小程序,要求可以做加减乘除运算

「java:编写一个计算器小程序,要求可以做加减乘除运算」热议话题订阅

方法1:微信扫描微信公开课小程序码即可订阅热议话题「java:编写一个计算器小程序,要求可以做加减乘除运算」

方法2:微信搜索微信公开课小程序名称进入,即可订阅热议话题「java:编写一个计算器小程序,要求可以做加减乘除运算」

方法3:微信网页访问即速商店,长按识别微信公开课小程序码即可订阅热议话题「java:编写一个计算器小程序,要求可以做加减乘除运算」

微信公开课小程序热议话题「java:编写一个计算器小程序,要求可以做加减乘除运算」由微信公开课原创摘录于微信小程序商店shop.jisuapp.cn,转载请注明出处。

微信公开课热议话题「java:编写一个计算器小程序,要求可以做加减乘除运算」由微信公开课开发者向微信用户提供,并对本服务内容、数据资料及其运营行为等真实性、合法性及有效性承担全部责任。