谁有java编的计算器代码给发个

王朝知道·作者佚名  2009-07-19
窄屏简体版  字體: |||超大  
 
分類: 電腦/網絡 >> 程序設計 >> 其他編程語言
 
參考答案:

12月22日 23:44 哈哈!我们也要交作业阿,老大把分给我吧,我没分啦!拷贝编译就行了我上礼拜写的

import java.awt.*;

import java.awt.event.*;

public class Calculator extends WindowAdapter implements ActionListener{

private double result=0,data1=0,radixPointDepth=1;

private boolean radixPointIndicate=false,resultIndicate=false;

private char prec='+';

private Frame f;

private TextField tf;

private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;

private Panel p;

static public void main(String args[]){

Calculator de=new Calculator();

de.go();

}

public void go(){

f=new Frame();

p=new Panel();

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

tf=new TextField(30);

b1=new Button("7");

b2=new Button("8");

b3=new Button("9");

b4=new Button("+");

b5=new Button("4");

b6=new Button("5");

b7=new Button("6");

b8=new Button("-");

b9=new Button("1");

b10=new Button("2");

b11=new Button("3");

b12=new Button("*");

b13=new Button("0");

b14=new Button(".");

b15=new Button("=");

b16=new Button("/");

f.add(tf,"North");

f.add(p,"Center");

p.add(b1);

p.add(b2);

p.add(b3);

p.add(b4);

p.add(b5);

p.add(b6);

p.add(b7);

p.add(b8);

p.add(b9);

p.add(b10);

p.add(b11);

p.add(b12);

p.add(b13);

p.add(b14);

p.add(b15);

p.add(b16);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

b10.addActionListener(this);

b11.addActionListener(this);

b12.addActionListener(this);

b13.addActionListener(this);

b14.addActionListener(this);

b15.addActionListener(this);

b16.addActionListener(this);

f.addWindowListener(this);

f.setSize(300,200);

f.setVisible(true);

}

public void actionPerformed(ActionEvent e){

String s;

s=e.getActionCommand();

switch(s.charAt(0)){

case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':

if(resultIndicate){

result=0;

data1=0;

}

Integer Int1=new Integer(s);

if(radixPointIndicate){

radixPointDepth=radixPointDepth/10;

data1=data1+(Int1.intValue())*radixPointDepth;

}

else{

data1=data1*10+(Int1.intValue());

}

Double displayNumber=new Double(data1);

tf.setText(displayNumber.toString());

resultIndicate=false;

break;

case '+': case '-':case '*':case '/':case '=':

if(s.charAt(0)!='='&&resultIndicate){

prec=s.charAt(0);

resultIndicate=false;

}

else{

switch(prec){

case '+':

result=result+data1;

break;

case '-':

result=result-data1;

break;

case '*':

result=result*data1;

break;

case '/':

result=result/data1;

break;

}

}

radixPointIndicate=false;

radixPointDepth=1;

displayNumber=new Double(result);

tf.setText(displayNumber.toString());

if(s.charAt(0)!='='){

data1=0;

prec=s.charAt(0);

}

else{

resultIndicate=true;

}

break;

case '.':

radixPointIndicate=true;

break;

}

}

public void windowClosing(WindowEvent e){

System.exit(0);

}

}

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航