当前位置:编程学习 > JAVA >>

简单计算器,实现混合运算错误,请告知原因啊

package jisuanqi;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class Jisuan extends JFrame implements ActionListener {
    JPanel jp;
    JTextField name,txt;
    JPasswordField pwd;    
    JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jb0,jbjia,jbjian,jbce,jbcu,jbd,jbda,jbqx;
    private String str="";
    private boolean first=true;
    float[] su,js;
    int i,j=0,k,d,m,n=0;
    float sum;
    String[] fh,gt;
Jisuan() throws Exception{
txt=new JTextField();
txt.setPreferredSize(new Dimension(30,50));
jbqx=new JButton("删除");
jbqx.setPreferredSize(new Dimension(30,50));
jbqx.addActionListener(this);
txt.setHorizontalAlignment(JTextField.RIGHT);
jp = new JPanel();
jp.setLayout(new GridLayout(4,4));
this.setLayout(new BorderLayout(40,0));
this.add(txt,BorderLayout.NORTH);
this.add(jbqx, BorderLayout.SOUTH);
jb1= new JButton("1");
jb1.addActionListener(this);
     jb2= new JButton("2");
     jb2.addActionListener(this);
     jb3= new JButton("3");
     jb3.addActionListener(this);
     jbjia= new JButton("+");
     jbjia.addActionListener(this);
     jb4= new JButton("4");
     jb4.addActionListener(this);  
     jb5 = new JButton("5");
     jb5.addActionListener(this);
     jb6 = new JButton("6");
     jb6.addActionListener(this);
     jbjian=new JButton("-");
     jbjian.addActionListener(this);
     jb7= new JButton("7");
     jb7.addActionListener(this);
     jb8= new JButton("8");
     jb8.addActionListener(this);
     jb9= new JButton("9");
     jb9.addActionListener(this);
     jbce=new JButton("*");
     jbce.addActionListener(this);
     jbd=new JButton(".");
     jbd.addActionListener(this);
     jb0=new JButton("0");
     jb0.addActionListener(this);
     jbda=new JButton("=");
     jbda.addActionListener(this);
     jbcu=new JButton("/");
     jbcu.addActionListener(this);
     jp.add(jb1);
     jp.add(jb2);    
     jp.add(jb3);
     jp.add(jbjia);
     jp.add(jb4);
     jp.add(jb5);
     jp.add(jb6);
     jp.add(jbjian);
     jp.add(jb7);
     jp.add(jb8);
     jp.add(jb9);
     jp.add(jbce);
     jp.add(jbd);
     jp.add(jb0);
     jp.add(jbda);
     jp.add(jbcu);
this.add(jp);
this.validate();
this.setTitle("计算器");
this.setBounds(100,100,400,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
    public void actionPerformed(ActionEvent e) {
     if(first){
       if(e.getActionCommand()=="+"||e.getActionCommand()=="-"||e.getActionCommand()=="*"||e.getActionCommand()=="/"||e.getActionCommand()=="="||e.getActionCommand()=="删除")
       {
       str="";
       txt.setText(str);
     }else if(e.getActionCommand()!="="){
     str=str.concat(e.getActionCommand());
     txt.setText(str);
     first=false;
     }
     }else if(e.getActionCommand()!="="){
        str=str.concat(e.getActionCommand());
      txt.setText(str);
      }
     else if(e.getActionCommand()=="="&&str!=""){
     String[] mw=str.split("\\+|\\-|\\*|\\/");
     for(i=0;i<mw.length;i+=2){
     su[i]=Float.parseFloat(mw[i]);
     System.out.printf(su[i]+"\n");
     }
     /*for(k=0;k<fh.length;k=d){
     d=k;
     while(fh[d]=="*"||fh[d]=="/"){
     if(fh[d]=="*"){
     su[d+1]=su[d]*su[d+1];
     }else{
     su[d+1]=su[d]/su[d+1];
     }
     d++;
     }
     }
     for(m=0;m<fh.length;m++){
     if(fh[m]=="+"||fh[m]=="-"){
     gt[n]=fh[m];
     js[n]=su[m];
     n++;
     }
     }
     js[n]=su[fh.length];
     for(n=0;n<gt.length;n++){
     if(fh[m]=="+"){
     js[n+1]=js[n]+js[n+1];
     }else{
     js[n+1]=js[n]-js[n+1];
     }
    
     }
     txt.setText("js[n+1]");*/
     }
    
     }
    public static void main(String []args) throws Exception{
     new Jisuan();    
    }
}
输入计算数据没有错误,但是在输入等号后开始计算出现错误;
提示为:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at jisuanqi.Jisuan.actionPerformed(Jisuan.java:102)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我的想法是将输入的变为字符串,在输入等号后,根据运算符+-*/分割字符串为字符串数组,然后将数字转换为数字类型,先算乘除,组成新的数组,再算加减,请告知错误在哪里了 java  计算器 四则运算 --------------------编程问答-------------------- 我认为你应该加一个监听类吧
class getButton implements ActionListener
,然后
jb2.addActionListener(new getButton());
,最主要的是对每个按钮做出响应方法,比如“=”,
if(e.getActionCommand()=="="){pressSum();}


String sign="null";//暂存符号数据

public void pressSum(){
if(sign=="null") 
number.setText(str); 
else if(sign=="add") 

num+=Double.parseDouble(str); 
str=Double.toString(num); 
number.setText(str); 

else if(sign=="sub") 

num-=Double.parseDouble(str); 
str=Double.toString(num); 
number.setText(str); 

else if(sign=="mul") 

num*=Double.parseDouble(str); 
str=Double.toString(num); 
number.setText(str); 

else if(sign=="div") 

if(num!=0) 

num/=Double.parseDouble(str); 
str=Double.toString(num); 
number.setText(str); 

else 

str="0"; 
number.setText(str); 


sign="null"; 

}  }

以上也是我从别处查到的,你可以试一下
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,