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

Java实现计算器

\

我的代码:


  1 // windows
  2
  3
  4 /*
  5 版本 1.0
  6
  7 显示一行数字
  8 简单二元运算
  9 数字为 double 型
 10 无输入数字位数限制
 11 可输入过多的小数点
 12
 13 界面:
 14 7 8 9 + div
 15 4 5 6 - mod
 16 1 2 3 * pow
 17 0 . = / ac
 18 */
 19
 20 import java.awt.*;
 21 import java.awt.event.*;
 22
 23 public class Calculator extends Frame implements ActionListener {
 24
 25         public static void main(String[] args) {
 26                 new Calculator();
 27         }
 28
 29         public Calculator() {
 30                 super( "计算器 -- 赵杰" );
 31
 32                 // keyBoard and key
 33                 key = new Button[ 300 ];
 34                 Panel kb = new Panel();
 35                 kb.setLayout( new GridLayout( 4, 5, 3, 3 ) );
 36                 int i;
 37
 38                 key[ i = (int)7 ] = new Button( "7" );
 39                 key[ i ].addActionListener( this );
 40                 kb.add( key[ i ] );
 41
 42                 key[ i = (int)8 ] = new Button( "8" );
 43                 key[ i ].addActionListener( this );
 44                 kb.add( key[ i ] );
 45
 46                 key[ i = (int)9 ] = new Button( "9" );
 47                 key[ i ].addActionListener( this );
 48                 kb.add( key[ i ] );
 49
 50                 key[ i = (int)+ ] = new Button( "+" );
 51                 key[ i ].addActionListener( this );
 52                 kb.add( key[ i ] );
 53
 54                 key[ i = (int)d ] = new Button( "div" );
 55                 key[ i ].addActionListener( this );
 56                 kb.add( key[ i ] );
 57
 58                 key[ i = (int)4 ] = new Button( "4" );
 59                 key[ i ].addActionListener( this );
 60                 kb.add( key[ i ] );
 61
 62                 key[ i = (int)5 ] = new Button( "5" );
 63                 key[ i ].addActionListener( this );
 64                 kb.add( key[ i ] );
 65
 66                 key[ i = (int)6 ] = new Button( "6" );
 67                 key[ i ].addActionListener( this );
 68                 kb.add( key[ i ] );
 69
 70                 key[ i = (int)- ] = new Button( "-" );
 71                 key[ i ].addActionListener( this );
 72                 kb.add( key[ i ] );
 73
 74                 key[ i = (int)m ] = new Button( "mod" );
 75                 key[ i ].addActionListener( this );
 76                 kb.add( key[ i ] );
 77
 78                 key[ i = (int)1 ] = new Button( "1" );
 79                 key[ i ].addActionListener( this );
 80                 kb.add( key[ i ] );
 81
 82                 key[ i = (int)2 ] = new Button( "2" );
 83                 key[ i ].addActionListener( this );
 84                 kb.add( key[ i ] );
 85
 86                 key[ i = (int)3 ] = new Button( "3" );
 87                 key[ i ].addActionListener( this );
 88                 kb.add( key[ i ] );
 89
 90                 key[ i = (int)* ] = new Button( "*" );
 91                 key[ i ].addActionListener( this );
 92                 kb.add( key[ i ] );
 93
 94

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,