急求一Java编写的类似计算机带的计算器的程序!!
3个回答
展开全部
No 1.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Calculator extends Applet implements ActionListener{
int i=0;
String op=" "; //存操作符
double num1=0,num2=0; //存操作数
boolean opr=false,num=false,sta=false,is=false; //设置状态
String btns[]={"7","8","9","/","C","4","5",
"6","X","√","1","2","3","-",
"%","0","+/-",".","+","="};
//设置布局
Panel pan1,pan2; //容器
BorderLayout bg1; //布局
GridLayout bg2;
Label input; //显示
Color c1,c2; //颜色变量
Button btn[]=new Button[20]; //按钮
public void init(){ //初始化
input=new Label();
pan1=new Panel();
pan2=new Panel();
bg1=new BorderLayout(0,10);
bg2=new GridLayout(0,5,6,8);
pan2.setLayout(bg2);
pan1.setLayout(bg1);
c1=new Color(151,118,203);
c2=new Color(164,210,249);
for(i=0;i<20;i++){
btn[i]=new Button(btns[i]);
pan2.add(btn[i]);
btn[i].setBackground(c2);
btn[i].addActionListener(this);
}
pan1.add(input,"North");
pan1.add(pan2,"Center");
input.setText("0");
input.setBackground(Color.pink);
input.setAlignment(Label.RIGHT);
this.setBackground(c1);
pan1.setBackground(c1);
add(pan1);
}
还有这个:
/*按0--9的按钮事件*/
else if( click.equals("0")||click.equals("1")||click.equals("2")||click.equals("3")||
click.equals("4")||click.equals("5")||click.equals("6")||click.equals("7")||
click.equals("8")||click.equals("9")){
pressnum(click);
if(op!=(" ")){ //设符号标志位
opr=true;
}
}
else if(click=="+"){ //加法
if(opr==false){
num1=Double.valueOf(input.getText()).doubleValue();
op="+";
num=false;
}
else {
num2=Double.valueOf(input.getText()).doubleValue();
if(op.equals("+")){
num1=num1+num2;
input.setText(isf(num1));
}
else if(op.equals("-")){
num1=num1-num2;
input.setText(isf(num1));
}
else if(op.equals("*")){
num1=num1*num2;
input.setText(isf(num1));
}
else if(op.equals("/")){
if(Double.toString(num2)!="0.0"){
num1=num1/num2;
input.setText(isf(num1));
}
else input.setText("不能用0做除数");
}
op="+";
opr=false;
num=false;
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Calculator extends Applet implements ActionListener{
int i=0;
String op=" "; //存操作符
double num1=0,num2=0; //存操作数
boolean opr=false,num=false,sta=false,is=false; //设置状态
String btns[]={"7","8","9","/","C","4","5",
"6","X","√","1","2","3","-",
"%","0","+/-",".","+","="};
//设置布局
Panel pan1,pan2; //容器
BorderLayout bg1; //布局
GridLayout bg2;
Label input; //显示
Color c1,c2; //颜色变量
Button btn[]=new Button[20]; //按钮
public void init(){ //初始化
input=new Label();
pan1=new Panel();
pan2=new Panel();
bg1=new BorderLayout(0,10);
bg2=new GridLayout(0,5,6,8);
pan2.setLayout(bg2);
pan1.setLayout(bg1);
c1=new Color(151,118,203);
c2=new Color(164,210,249);
for(i=0;i<20;i++){
btn[i]=new Button(btns[i]);
pan2.add(btn[i]);
btn[i].setBackground(c2);
btn[i].addActionListener(this);
}
pan1.add(input,"North");
pan1.add(pan2,"Center");
input.setText("0");
input.setBackground(Color.pink);
input.setAlignment(Label.RIGHT);
this.setBackground(c1);
pan1.setBackground(c1);
add(pan1);
}
还有这个:
/*按0--9的按钮事件*/
else if( click.equals("0")||click.equals("1")||click.equals("2")||click.equals("3")||
click.equals("4")||click.equals("5")||click.equals("6")||click.equals("7")||
click.equals("8")||click.equals("9")){
pressnum(click);
if(op!=(" ")){ //设符号标志位
opr=true;
}
}
else if(click=="+"){ //加法
if(opr==false){
num1=Double.valueOf(input.getText()).doubleValue();
op="+";
num=false;
}
else {
num2=Double.valueOf(input.getText()).doubleValue();
if(op.equals("+")){
num1=num1+num2;
input.setText(isf(num1));
}
else if(op.equals("-")){
num1=num1-num2;
input.setText(isf(num1));
}
else if(op.equals("*")){
num1=num1*num2;
input.setText(isf(num1));
}
else if(op.equals("/")){
if(Double.toString(num2)!="0.0"){
num1=num1/num2;
input.setText(isf(num1));
}
else input.setText("不能用0做除数");
}
op="+";
opr=false;
num=false;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询