Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 寻求好心人帮我修改一下这个程序
zwguo1008





发贴: 2
积分: 0
于 2007-11-01 10:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
本人想编个简单的计算器,可老有个异常,寻求好心人帮解决一下,不要在源程序上有大的改动哦,谢谢了
程序如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class jisuanqi extends JFrame implements ActionListener
{
  JTextField displaytext=new JTextField(30);
  String[] keys={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","=","/","C"};
  JButton[] Button;
  String numstr1;
  String numstr2;
  char op;
  boolean firstinput=true;
  public jisuanqi()
  {
    setTitle("我的计算器");
    setSize(260,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout layout=new GridLayout(3,1,10,10);
   JPanel row1=new JPanel();
   row1.add(displaytext);
   add(row1);
   GridLayout layout1=new GridLayout(4,4,10,10);
   JPanel row2=new JPanel();
   row2.setLayout(layout1);
   for(int index=0;index<16;index++)
   {
     Button[index]=new JButton(keys[index]);
     Button[index].addActionListener(this);
     Button[index].setSize(50,30);
     row2.add(Button[index]);
   }
   add(row2);
   JPanel row3=new JPanel();
   Button[16]=new JButton(keys[16]);
   Button[16].addActionListener(this);
   row3.add(Button[16]);
   add(row3);
   setVisible(true);
  }
  public void actionPerformed(ActionEvent e)
  {
    String resultstr;
    String str=e.getActionCommand();
    char ch;
    ch=str.charAt(0);
    switch(ch)
    {
      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
      case '.': if(firstinput)
       {
         numstr1=numstr1+ch;
         displaytext.setText(numstr1);
       }
       else
       {
         numstr2=numstr2+ch;
         displaytext.setText(numstr2);
       }
       break;
      case '+':
      case '-':
      case '*':
      case '/': op=ch;
       firstinput=false;
       break;
      case '=': resultstr=jisuan();
       displaytext.setText(resultstr);
       numstr1=resultstr;
       numstr2="";
       break;
      case 'C': displaytext.setText("0.");
       numstr1="";
       numstr2="";
       break;
      }
    }
    public String jisuan()
    {
      
      try
      {
        double A=  Double.valueOf(numstr1);
        double B=  Double.valueOf(numstr1);
        double result=0.0;
        switch(op)
        {
          case '+': result=A+B;
           break;
         case '-': result=A-B;
         break;
         case '*': result=A*B;
         break;
         case '/': result=A/B;
         break;
       }
       return String.valueOf(result);
     }  
     catch(ArithmeticException e)
     {
       
       return "错误"+e.getMessage();
     }
     catch(NumberFormatException e)
     {
    
       if(numstr1.equals(""))
        return "错误:第一个数不可用";
       else
        return "错误:第二个数不可用";
     }
     catch(Exception e)
     {
       
        return "错误";
     }
    
    
  }
  public static void main(String[] args)
  {
    jisuanqi myjisuanqi=new jisuanqi();
  }
}




致JAVA初学者+如何下手学JAVA

话题树型展开
人气 标题 作者 字数 发贴时间
6506 寻求好心人帮我修改一下这个程序 zwguo1008 3013 2007-11-01 10:19
5525 Re:寻求好心人帮我修改一下这个程序 andy_wang_5 3442 2007-11-01 11:04
5237 Re:寻求好心人帮我修改一下这个程序 andy_wang_5 87 2007-11-01 11:13
5212 Re:寻求好心人帮我修改一下这个程序 chenshuai123 2477 2007-11-05 00:17

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923