java计算器程序老是出错,那位高手帮忙改一下,谢谢,急用

importjava.awt.*;importjava.awt.event.*;publicclassCalculatorextendsFrameimplementsAc... import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame implements ActionListener{
private Frame f;
private double result;
private boolean operated=false;
private Button btn[]=new Button[17];
private TextField tf;
public Calculator(){
f=new Frame("Calculator");
f.setLayout(new BorderLayout());
f.setSize(190,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
Panel centerPanel=new centerPanel();
centerPanel.setLayout(new GirdLayout(5,3));
btn[10]=new Button("+");
btn[11]=new Button("-");
btn[12]=new Button("*");
btn[13]=new Button("/");
btn[14]=new Button("√");
btn[15]=new Button("∧");
btn[16]=new Button("=");
for(int i=0;i<10;i++){
btn[i]=new Button(String.valueOf(i));
}
for(int i=0;i<=14;i++){centerPanel.add(btn[i]); }
for(int i=0;i<=14;i++){if(i%2==1){centerPanel.add(btn[(i+19)/2]);}
f.add(centerPanel,BorderLayout.CENTER);
Panel southPanel=new southPanel();
FlowLayout flow=new FlowLayout();
southPanel.setLayout(flow);
southPanel.add(btn[15]);
southPanel.add(btn[16]);
f.add(BorderLayout.SOUTH,southPanel);
Panel northPanel=new northPanel();
tf=new TextField(20);
tf.setEditable(false);
northPanel.add(tf);
f.add(northPanel,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e){
double currentNunber=Double.valueOf(tf.getText()).doubleValue();
if(e.getSource()==btn[10]){result+=currentNumber;operated=false;}
else if(e.getSource()==btn[11]){result-=currentNumber;operated=false;}
else if(e.getSource()==btn[12]){result*=currentNumber;operated=false;}
else if(e.getSource()==btn[13]){if(currentNumber==0) result=0;else result/=currentNumber;operated=false;}
else if(e.getSource()==btn[14]){result=Math.sqrt(currentNumber);operated=true;result=currentNumber;}
else if(e.getSource()==btn[15]){result=Math.pow(currentNumber,2);operated=true;result=currentNumber;}
else if(e.getSource()==btn[16]){operated=true;if(operated){result=currentNumber;}}
else{
if(operated)
result=0;
for(int i=0;i<10;i++)
if(e.getSource()==btn[i]){
if(tf.getText().equals("0"))
tf.setText(""+i);
else if(!operated)
tf.setText(tf.getText()+"i");
else{tf.setText(""+i);operated=false;}
}
}
}
public static void main(String args[]){
new Calculator();}
}
public void actionPerformed(ActionEvent e){
运行显示这一行有12个错误
展开
 我来答
碧素洗尘
推荐于2016-02-23 · 超过19用户采纳过TA的回答
知道答主
回答量:65
采纳率:0%
帮助的人:44.8万
展开全部
我有点忙,所以没时间给你慢慢改。不过我这儿有一个计算器的代码,你不介意的话用吧!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator implements ActionListener{
private double data1=0.0,data2=0.0;
private String t_content;
boolean number=false;

//key用来标示用户所按下的按钮即加减乘除
//key为0标示+,为1表示-,为2表示*,为3表示/
short key=-1;

//pkey的取值为-1~5
//pkey为-1表示第一次按下=按钮,为5说明不是第一次按下=按钮
//pkey为其他值0,1,2,3时分别代表+,-,*,/
short pkey=-1;

Frame frame=new Frame("Calculator");//定义标题为Calculator的窗体
TextField textfield=new TextField(30);//定义计算机

//定义backspace,ce,c0按钮
Button backspace=new Button("Backspace");
Button ce=new Button("CE");
Button c0=new Button("C");

//定义面板,其中backspace,ce,c0按钮和计算机兰textfeild放在面板p1中
//其余按钮放在面板p2中
Panel p1=new Panel();
Panel p2=new Panel();

//定义界面上的按钮数组,即除去backspace,ce,c0按钮的所有按钮
String names[]={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
Button bb[]=new Button[names.length];

public static void main(String[] args){
Calculator cal=new Calculator();
cal.go();
}

public void go(){
frame.setSize(300,200);
frame.setLayout(new BorderLayout());
Font fonts=new Font("楷体_GB2312",Font.PLAIN,12);

//设置面板p1的字体和布局管理类型,将textfeild对象添加到面板中
p1.setFont(fonts);
p1.setLayout(new GridLayout(2,1,5,10));
textfield.setFont(fonts);
p1.add(textfield,null);

//backspace,ce,c0按钮注册addActionListener方法
backspace.addActionListener(this);
ce.addActionListener(this);
c0.addActionListener(this);

//将backspace,ce,c0按钮添加到p1面板中
p1.add(backspace,null);
p1.add(ce,null);
p1.add(c0,null);

//设置面板p2的布局管理器类型为GridLayout
p2.setLayout(new GridLayout(4,5,5,5));

//为按钮组中的按钮注册addActionListener方法
for(int i=0;i<names.length;i++){
bb[i]=new Button(names[i]);
p2.add(bb[i]);
bb[i].addActionListener(this);
}

//设置面板p1和面板p2在窗体中的位置
//面板p1在窗体的North方向,面板p2在窗体的South方向
frame.add(p1,BorderLayout.NORTH);
frame.add(p2,BorderLayout.SOUTH);
frame.setVisible(true);

//当单击关闭按钮时退出系统
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae){
String s;
//通过ActionEvent类的getActionCommand()方法,得出动作是由哪个按钮触发的。 s=ae.getActionCommand();

//用户单击一次,计算栏里便去掉一个字符
//先将计算栏里的字符清空,再添加除最好一个字符的字符串
if(s.equals("Backspace")&&number){
t_content=textfield.getText();
textfield.setText("");

for(int i=0;i<t_content.length()-1;i++){
char c=t_content.charAt(i);
textfield.setText(textfield.getText()+c);
}
}

//用户单击的是CE按钮,计算栏清空
if(s.equals("CE")){
textfield.setText("");
number=false;
}

//用户单击的是C按钮
if(s.equals("C")){
textfield.setText("");
data1=data2=0.0;
number=false;
}

//用户单击的是sqrt按钮,其中负数不能被开方
if(s.equals("sqrt")&&number){
t_content=textfield.getText();
if(t_content.charAt(0)=='-'){
textfield.setText("负数不能被开方");
number=false;
}else{
textfield.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(t_content))));
}
}

//用户单击的是1/x求倒数按钮,求倒数时分母不能为0
if(s.equals("1/x") && number){
t_content=textfield.getText();
if(t_content.equals("0")){ textfield.setText("零不能求倒数");
number=false;
}else{
textfield.setText(Double.toString(1/Double.parseDouble(t_content)));
}
}

//用户单击的是%按钮
if(s.equals("%")&&number){
if(data2==0){
t_content=textfield.getText();
textfield.setText(t_content);
}else{
boolean g=false;
for(int i=0;i<textfield.getText().length();i++)
if('.'==textfield.getText().charAt(i)){
g=true;
break;
}

//如果是Double 数,除100
if(g=true){
double dtemp=Double.parseDouble(textfield.getText());
dtemp=dtemp/100.0;
textfield.setText(Double.toString(dtemp));
}

//如果是int数但能被100整除,则去掉末尾两个0
else{
if(Integer.parseInt(textfield.getText())%100==0){
int itemp=Integer.parseInt(textfield.getText());
itemp/=100;
textfield.setText(Integer.toString(itemp));
}

//如果是int数,但不能被100整除,则按Double数处理
else{
double dtemp=Double.parseDouble(textfield.getText());
dtemp=dtemp/100.0;
textfield.setText(Double.toString(dtemp));
}
}
} }

//对正负数字的处理
if(s.equals("+/-")){
t_content=textfield.getText();
if(t_content.charAt(0)=='-'){
textfield.setText("");
String content;
for(int i=1;i<t_content.length();i++){
textfield.setText(textfield.getText()+t_content.charAt(i));
}
}else{
textfield.setText("-"+t_content);
}
}

//所按下的按钮为+按钮
if(s.equals("+")){
switch(pkey){
case 0:
data2+=Double.parseDouble(textfield.getText());
break;
case 1:
data2+=Double.parseDouble(textfield.getText());
break;
case 2:
data2+=Double.parseDouble(textfield.getText());
break;
case 3:
if(textfield.getText().equals("0")){
textfield.setText("除数不能为零");
number=false;
}else{
key=-1;
data2/=data1;
textfield.setText(Double.toString(data2));
break;
}
default:
data2=Double.parseDouble(textfield.getText());
}
pkey=key=0;
textfield.setText("");
}
//所按下的按钮为-按钮

if(s.equals("-")){
switch(pkey){
case 0:
data2+=Double.parseDouble(textfield.getText());
break;
case 1:
data2+=Double.parseDouble(textfield.getText());
break;
case 2:
data2+=Double.parseDouble(textfield.getText());
break;

case 3:
if(textfield.getText().equals("0")){
textfield.setText("除数不能为零");
number=false;
}else{
key=-1;
data2/=data1;
textfield.setText(Double.toString(data2));
break;
}
default:
data2=Double.parseDouble(textfield.getText());
}
pkey=key=1;
textfield.setText("");
}

//所按下的按钮为*按钮
if(s.equals("*")){
switch(pkey){
case 0:
data2+=Double.parseDouble(textfield.getText());
break;
case 1:
data2+=Double.parseDouble(textfield.getText());
break;
case 2:
data2+=Double.parseDouble(textfield.getText());
break;

case 3:
if(textfield.getText().equals("0")){ textfield.setText("除数不能为零");
number=false;
}else{
key=-1;
data2/=data1;
textfield.setText(Double.toString(data2));
break;
}
default:
data2=Double.parseDouble(textfield.getText());
}
pkey=key=2;
textfield.setText("");
}

//所按下的按钮为/按钮
if(s.equals("/")){
switch(pkey){
case 0:
data2+=Double.parseDouble(textfield.getText());
break;
case 1:
data2+=Double.parseDouble(textfield.getText());
break;
case 2:
data2+=Double.parseDouble(textfield.getText());
break;

case 3:
if(textfield.getText().equals("0")){
textfield.setText("除数不能为零");
number=false;
}else{
key=-1;
data2/=data1;
textfield.setText(Double.toString(data2));
break;
}
default:
data2=Double.parseDouble(textfield.getText());
}
pkey=key=3;
textfield.setText("");
}

//所按下的按钮为=,求结果
if(s.equals("=")&&number){

//如果不是第一次按下=按钮
if(pkey==5){
switch(pkey){
case 0:
data2+=data1;
textfield.setText(Double.toString(data2));
break;
case 1:
data2-=data1;
textfield.setText(Double.toString(data2));
break;
case 2:
data2*=data1;
textfield.setText(Double.toString(data2));
break;
case 3:
if(textfield.getText().equals("0")){
textfield.setText("除数不能为零");
number=false;
}else{

data2/=data1;
textfield.setText(Double.toString(data2));
break;
}
}
}

//如果是第一次按下=按钮
else{
data1=Double.parseDouble(textfield.getText());
switch(key){
case 0:
key=-1;
data2+=Double.parseDouble(textfield.getText());
textfield.setText(Double.toString(data2));
break;

case 1:
key=-1;
data2-=Double.parseDouble(textfield.getText());
textfield.setText(Double.toString(data2)); break;
case 2:
key=-1;
data2*=Double.parseDouble(textfield.getText());
textfield.setText(Double.toString(data2));
break;
case 3:
if(textfield.getText().equals("0")){
textfield.setText("除数不能为零");
number=false;
}else{
key=-1;
data2/=Double.parseDouble(textfield.getText());
textfield.setText(Double.toString(data2));
break;
}
}
}
pkey=5;
}

//按下的按钮时数字按钮
else{
switch(s.charAt(0)){
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
number=true;
textfield.setText(textfield.getText()+s);
break;
//对.的处理
case'.':
t_content=textfield.getText();
boolean g=false; //对计算机栏中的数据进行检验
//如果其中有小数点,则用户单击该按钮时不在添加小数点
for(int i=0;i<t_content.length();i++){
if(t_content.charAt(i)=='.'){
g=true;
break;
}
}

//数据中已经有小数点
if(g==true)
break;
// 数据中没有小数点,当用户单击小数点时添加小数点
if(g==false)
textfield.setText(t_content+".");
}
}
}

private String Integer(String string) {
// TODO Auto-generated method stub
return null;
}
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友fd1deed55
2010-11-02
知道答主
回答量:52
采纳率:0%
帮助的人:0
展开全部
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Calculator extends Frame implements ActionListener{
private Frame f;
private double result;
private boolean operated=false;
private Button btn[]=new Button[17];
private TextField tf;
public Calculator(){
f=new Frame("Calculator");
f.setLayout(new BorderLayout());
f.setSize(190,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Panel centerPanel=new Panel();
centerPanel.setLayout(new GridLayout(5,3));
btn[10]=new Button("+");
btn[11]=new Button("-");
btn[12]=new Button("*");
btn[13]=new Button("/");
btn[14]=new Button("√");
btn[15]=new Button("∧");
btn[16]=new Button("=");
for(int i=0;i<10;i++){
btn[i]=new Button(String.valueOf(i));
}
for(int i=0;i<=14;i++){centerPanel.add(btn[i]); }
for(int i=0;i<=14;i++){if(i%2==1){centerPanel.add(btn[(i+19)/2]);}
f.add(centerPanel,BorderLayout.CENTER);
Panel southPanel=new Panel();
FlowLayout flow=new FlowLayout();
southPanel.setLayout(flow);
southPanel.add(btn[15]);
southPanel.add(btn[16]);
f.add(BorderLayout.SOUTH,southPanel);
Panel northPanel=new Panel();
tf=new TextField(20);
tf.setEditable(false);
northPanel.add(tf);
f.add(northPanel,BorderLayout.NORTH);
}
}
public void actionPerformed(ActionEvent e){
double currentNumber=Double.valueOf(tf.getText()).doubleValue();
if(e.getSource()==btn[10]){result+=currentNumber;operated=false;}
else if(e.getSource()==btn[11]){result-=currentNumber;operated=false;}
else if(e.getSource()==btn[12]){result*=currentNumber;operated=false;}
else if(e.getSource()==btn[13]){if(currentNumber==0) result=0;else result/=currentNumber;operated=false;}
else if(e.getSource()==btn[14]){result=Math.sqrt(currentNumber);operated=true;result=currentNumber;}
else if(e.getSource()==btn[15]){result=Math.pow(currentNumber,2);operated=true;result=currentNumber;}
else if(e.getSource()==btn[16]){operated=true;if(operated){result=currentNumber;}}
else{
if(operated)
result=0;
for(int i=0;i<10;i++)
if(e.getSource()==btn[i]){
if(tf.getText().equals("0"))
tf.setText(""+i);
else if(!operated)
tf.setText(tf.getText()+"i");
else{tf.setText(""+i);operated=false;}
}
}
}

public static void main(String args[]){
new Calculator();}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
i178269245
2010-10-31 · TA获得超过185个赞
知道小有建树答主
回答量:180
采纳率:0%
帮助的人:115万
展开全部
贴错误信息啊 真是 还让人一行行看啊
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式