请教高手我这个java小计算器的程序哪里出错了,为什么算不出结果?程序如下
importjava.awt.Button;importjava.awt.event.*;importjava.awt.*;importjava.awt.*;import...
import java.awt.Button;
import java.awt.event.*;
import java.awt.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class 作业 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new AppFrame(); }
class AppFrame extends Frame
{
String s1;
TextField out=new TextField(30);
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
Button b6=new Button("6");
Button b7=new Button("7");
Button b8=new Button("8");
Button b9=new Button("9");
Button b0=new Button("0");
Button op1=new Button("+");
Button op2=new Button("-");
Button op3=new Button("*");
Button op4=new Button("/");
Button op5=new Button("=");
public AppFrame()
{
setLayout(new FlowLayout());
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);
add(op1);
add(op2);
add(op3);
add(op4);
add(op5);
add(out);
b1.addActionListener(new BActionAdapter());
b2.addActionListener(new BActionAdapter());
b3.addActionListener(new BActionAdapter());
b4.addActionListener(new BActionAdapter());
b5.addActionListener(new BActionAdapter());
b6.addActionListener(new BActionAdapter());
b7.addActionListener(new BActionAdapter());
b8.addActionListener(new BActionAdapter());
b9.addActionListener(new BActionAdapter());
b0.addActionListener(new BActionAdapter());
op1.addActionListener(new BActionAdapter());
op2.addActionListener(new BActionAdapter());
op3.addActionListener(new BActionAdapter());
op4.addActionListener(new BActionAdapter());
op5.addActionListener(new BActionAdapter());
setSize(300,300);
show();}
class BActionAdapter implements ActionListener
{double a;
boolean t;
double b;
String op;
String s1;
public void actionPerformed(ActionEvent e)
{String click=e.getActionCommand();
out.setText(out.getText()+click);
if(!click.equals("+")&&!click.equals("-")&&!click.equals("*")&&!click.equals("/")&&!click.equals("="))
{if(s1==null)//判断是否是第一个数
s1=click;
else
s1+=click;
a=new Double(s1).doubleValue();
while(t=true)
{ if(op.equals("+"))
a=a+b;
else if(op.equals("-"))
a=b-a;
else if(op.equals("*"))
a=a*b;
else if(op.equals("/"))
a=b/a; }}
else if(!click.equals("="))
{op=click;
s1=null;
b=a;
t=true;}
else{out.setText(out.getText()+a);
t=false;}
}
}
}}
是在eclipse下编写的 展开
import java.awt.event.*;
import java.awt.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class 作业 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new AppFrame(); }
class AppFrame extends Frame
{
String s1;
TextField out=new TextField(30);
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
Button b6=new Button("6");
Button b7=new Button("7");
Button b8=new Button("8");
Button b9=new Button("9");
Button b0=new Button("0");
Button op1=new Button("+");
Button op2=new Button("-");
Button op3=new Button("*");
Button op4=new Button("/");
Button op5=new Button("=");
public AppFrame()
{
setLayout(new FlowLayout());
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);
add(op1);
add(op2);
add(op3);
add(op4);
add(op5);
add(out);
b1.addActionListener(new BActionAdapter());
b2.addActionListener(new BActionAdapter());
b3.addActionListener(new BActionAdapter());
b4.addActionListener(new BActionAdapter());
b5.addActionListener(new BActionAdapter());
b6.addActionListener(new BActionAdapter());
b7.addActionListener(new BActionAdapter());
b8.addActionListener(new BActionAdapter());
b9.addActionListener(new BActionAdapter());
b0.addActionListener(new BActionAdapter());
op1.addActionListener(new BActionAdapter());
op2.addActionListener(new BActionAdapter());
op3.addActionListener(new BActionAdapter());
op4.addActionListener(new BActionAdapter());
op5.addActionListener(new BActionAdapter());
setSize(300,300);
show();}
class BActionAdapter implements ActionListener
{double a;
boolean t;
double b;
String op;
String s1;
public void actionPerformed(ActionEvent e)
{String click=e.getActionCommand();
out.setText(out.getText()+click);
if(!click.equals("+")&&!click.equals("-")&&!click.equals("*")&&!click.equals("/")&&!click.equals("="))
{if(s1==null)//判断是否是第一个数
s1=click;
else
s1+=click;
a=new Double(s1).doubleValue();
while(t=true)
{ if(op.equals("+"))
a=a+b;
else if(op.equals("-"))
a=b-a;
else if(op.equals("*"))
a=a*b;
else if(op.equals("/"))
a=b/a; }}
else if(!click.equals("="))
{op=click;
s1=null;
b=a;
t=true;}
else{out.setText(out.getText()+a);
t=false;}
}
}
}}
是在eclipse下编写的 展开
4个回答
展开全部
逻辑错误太多了
1.先顶2楼
2.a,b,t,op均未赋初值,第一次按数字的时候op未赋值,会报空指针异常
3.第一个if语句剔除了运算符和=号,所以
if (op.equals("+")) a = a + b;
else if (op.equals("-")) a = b - a;
else if (op.equals("*")) a = a * b;
else if (op.equals("/")) a = b / a;
这里所有的if内的代码永远都没法调到
4.未定义关闭事件监听,无法正常关闭窗口
给你个简单点的例子:添加了清空功能和关闭窗口,改进了计算器逻辑
当然缺点也有:未处理除数为0的情况,不能处理负数,计算完成后必须点clear才能进行下次计算
static class AppFrame extends Frame {
//你自己的button我就不写了
Button clear = new Button("clear");
public AppFrame()
{
setLayout(new FlowLayout());
//你自己的button我就不写了
add(clear);
//你自己的button我就不写了
clear.addActionListener(new BActionAdapter());
setSize(300, 300);
this.addWindowListener(new WindowAdapter() {//关闭事件
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
// show();
}
double a=0;
double b=0;
String op=null;
class BActionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e) {
String click = e.getActionCommand();
if(click.equals("clear")){//清空进行下次运算
a=0;
b=0;
op=null;
out.setText("");
}
else if(!click.equals("=")){//输入不是=号
if(click.charAt(0)<'0'){//输入为+-*/,获得第一个操作数及操作符
a=Double.parseDouble(out.getText());
op=click;
}
out.setText(out.getText()+click);
}
else{//输入为=号
//获得第二个操作数
b=Double.parseDouble(out.getText().substring(//根据 数字 起始位置获得 数字的字符串
out.getText().indexOf(op)+1)//根据操作符位置算出第二个操作数起始位置
);
if (op.equals("+"))
a = a + b;
else if (op.equals("-"))
a = a - b;
else if (op.equals("*"))
a = a * b;
else if (op.equals("/"))
a = a / b;
out.setText(out.getText()+"="+Double.toString(a));
}
}
}
}
1.先顶2楼
2.a,b,t,op均未赋初值,第一次按数字的时候op未赋值,会报空指针异常
3.第一个if语句剔除了运算符和=号,所以
if (op.equals("+")) a = a + b;
else if (op.equals("-")) a = b - a;
else if (op.equals("*")) a = a * b;
else if (op.equals("/")) a = b / a;
这里所有的if内的代码永远都没法调到
4.未定义关闭事件监听,无法正常关闭窗口
给你个简单点的例子:添加了清空功能和关闭窗口,改进了计算器逻辑
当然缺点也有:未处理除数为0的情况,不能处理负数,计算完成后必须点clear才能进行下次计算
static class AppFrame extends Frame {
//你自己的button我就不写了
Button clear = new Button("clear");
public AppFrame()
{
setLayout(new FlowLayout());
//你自己的button我就不写了
add(clear);
//你自己的button我就不写了
clear.addActionListener(new BActionAdapter());
setSize(300, 300);
this.addWindowListener(new WindowAdapter() {//关闭事件
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
// show();
}
double a=0;
double b=0;
String op=null;
class BActionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e) {
String click = e.getActionCommand();
if(click.equals("clear")){//清空进行下次运算
a=0;
b=0;
op=null;
out.setText("");
}
else if(!click.equals("=")){//输入不是=号
if(click.charAt(0)<'0'){//输入为+-*/,获得第一个操作数及操作符
a=Double.parseDouble(out.getText());
op=click;
}
out.setText(out.getText()+click);
}
else{//输入为=号
//获得第二个操作数
b=Double.parseDouble(out.getText().substring(//根据 数字 起始位置获得 数字的字符串
out.getText().indexOf(op)+1)//根据操作符位置算出第二个操作数起始位置
);
if (op.equals("+"))
a = a + b;
else if (op.equals("-"))
a = a - b;
else if (op.equals("*"))
a = a * b;
else if (op.equals("/"))
a = a / b;
out.setText(out.getText()+"="+Double.toString(a));
}
}
}
}
展开全部
你写的有问题,我可以帮你改成可正常运行的。不过逻辑上就不给你改了,你自已再去琢磨琢磨吧
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new AppFrame();
}
} // 这里,不要写成内部类
class AppFrame extends Frame {
String s1;
TextField out = new TextField(30);
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button b0 = new Button("0");
Button op1 = new Button("+");
Button op2 = new Button("-");
Button op3 = new Button("*");
Button op4 = new Button("/");
Button op5 = new Button("=");
public AppFrame()
{
setLayout(new FlowLayout());
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);
add(op1);
add(op2);
add(op3);
add(op4);
add(op5);
add(out);
b1.addActionListener(new BActionAdapter());
b2.addActionListener(new BActionAdapter());
b3.addActionListener(new BActionAdapter());
b4.addActionListener(new BActionAdapter());
b5.addActionListener(new BActionAdapter());
b6.addActionListener(new BActionAdapter());
b7.addActionListener(new BActionAdapter());
b8.addActionListener(new BActionAdapter());
b9.addActionListener(new BActionAdapter());
b0.addActionListener(new BActionAdapter());
op1.addActionListener(new BActionAdapter());
op2.addActionListener(new BActionAdapter());
op3.addActionListener(new BActionAdapter());
op4.addActionListener(new BActionAdapter());
op5.addActionListener(new BActionAdapter());
setSize(300, 300);
show();
}
// 这里变量都放到外部
double a;
boolean t;
double b;
String op;
class BActionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e) {
String click = e.getActionCommand();
out.setText(out.getText() + click);
if (!click.equals("+") && !click.equals("-") && !click.equals("*") && !click.equals("/")
&& !click.equals("=")) {
if (s1 == null) // 判断是否是第一个数
s1 = click;
else s1 += click;
a = new Double(s1).doubleValue();
if (t == true) { // 这里,用于判断而不是赋值,且只用if不能用while
if (op.equals("+")) a = a + b;
else if (op.equals("-")) a = b - a;
else if (op.equals("*")) a = a * b;
else if (op.equals("/")) a = b / a;
}
}
else if (!click.equals("=")) {
op = click;
s1 = null;
b = a;
t = true;
}
else {
out.setText(out.getText() + a);
t = false;
}
}
}
}
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new AppFrame();
}
} // 这里,不要写成内部类
class AppFrame extends Frame {
String s1;
TextField out = new TextField(30);
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button b0 = new Button("0");
Button op1 = new Button("+");
Button op2 = new Button("-");
Button op3 = new Button("*");
Button op4 = new Button("/");
Button op5 = new Button("=");
public AppFrame()
{
setLayout(new FlowLayout());
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);
add(op1);
add(op2);
add(op3);
add(op4);
add(op5);
add(out);
b1.addActionListener(new BActionAdapter());
b2.addActionListener(new BActionAdapter());
b3.addActionListener(new BActionAdapter());
b4.addActionListener(new BActionAdapter());
b5.addActionListener(new BActionAdapter());
b6.addActionListener(new BActionAdapter());
b7.addActionListener(new BActionAdapter());
b8.addActionListener(new BActionAdapter());
b9.addActionListener(new BActionAdapter());
b0.addActionListener(new BActionAdapter());
op1.addActionListener(new BActionAdapter());
op2.addActionListener(new BActionAdapter());
op3.addActionListener(new BActionAdapter());
op4.addActionListener(new BActionAdapter());
op5.addActionListener(new BActionAdapter());
setSize(300, 300);
show();
}
// 这里变量都放到外部
double a;
boolean t;
double b;
String op;
class BActionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e) {
String click = e.getActionCommand();
out.setText(out.getText() + click);
if (!click.equals("+") && !click.equals("-") && !click.equals("*") && !click.equals("/")
&& !click.equals("=")) {
if (s1 == null) // 判断是否是第一个数
s1 = click;
else s1 += click;
a = new Double(s1).doubleValue();
if (t == true) { // 这里,用于判断而不是赋值,且只用if不能用while
if (op.equals("+")) a = a + b;
else if (op.equals("-")) a = b - a;
else if (op.equals("*")) a = a * b;
else if (op.equals("/")) a = b / a;
}
}
else if (!click.equals("=")) {
op = click;
s1 = null;
b = a;
t = true;
}
else {
out.setText(out.getText() + a);
t = false;
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
boolean t;
........
while(t=true)
你编译能通过?
另外请先把代码格式化一下,看起来太乱了
........
while(t=true)
你编译能通过?
另外请先把代码格式化一下,看起来太乱了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我给你改了改 能正确计算结果了:两位已经ok了 当然还有其它各种问题,我这只是保证你第一次进去可以计算正确
代码如下
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ZuoYe {
/**
* @param args
*/
public static void main(String[] args) {
new AppFrame();
}
}
@SuppressWarnings("serial")
class AppFrame extends Frame {
String s1;
TextField out = new TextField(30);
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button b0 = new Button("0");
Button op1 = new Button("+");
Button op2 = new Button("-");
Button op3 = new Button("*");
Button op4 = new Button("/");
Button op5 = new Button("=");
public AppFrame()
{
setLayout(new FlowLayout());
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);
add(op1);
add(op2);
add(op3);
add(op4);
add(op5);
add(out);
b1.addActionListener(new BActionAdapter());
b2.addActionListener(new BActionAdapter());
b3.addActionListener(new BActionAdapter());
b4.addActionListener(new BActionAdapter());
b5.addActionListener(new BActionAdapter());
b6.addActionListener(new BActionAdapter());
b7.addActionListener(new BActionAdapter());
b8.addActionListener(new BActionAdapter());
b9.addActionListener(new BActionAdapter());
b0.addActionListener(new BActionAdapter());
op1.addActionListener(new BActionAdapter());
op2.addActionListener(new BActionAdapter());
op3.addActionListener(new BActionAdapter());
op4.addActionListener(new BActionAdapter());
op5.addActionListener(new BActionAdapter());
setSize(300, 300);
show();
}
String op;
double a;
double b;
class BActionAdapter implements ActionListener {
boolean t;
public void actionPerformed(ActionEvent e) {
String click = e.getActionCommand();
out.setText(out.getText() + click);
if (!click.equals("+") && !click.equals("-") && !click.equals("*") && !click.equals("/") && !click.equals("=")) {
if (s1 == null)// 判断是否是第一个数
s1 = click;
else
s1 += click;
a = new Double(s1).doubleValue();
}
else if (!click.equals("=")) {
if (click.equals("+") || !click.equals("-") || !click.equals("*") || !click.equals("/")) {
op = click;
s1 = null;
}
b = a;
t = true;
}
else {
if (op != null) {
if (op.equals("+"))
a = a + b;
else if (op.equals("-"))
a = b - a;
else if (op.equals("*"))
a = a * b;
else if (op.equals("/"))
a = b / a;
}
out.setText(out.getText() + a);
t = false;
}
}
}
}
代码如下
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ZuoYe {
/**
* @param args
*/
public static void main(String[] args) {
new AppFrame();
}
}
@SuppressWarnings("serial")
class AppFrame extends Frame {
String s1;
TextField out = new TextField(30);
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button b0 = new Button("0");
Button op1 = new Button("+");
Button op2 = new Button("-");
Button op3 = new Button("*");
Button op4 = new Button("/");
Button op5 = new Button("=");
public AppFrame()
{
setLayout(new FlowLayout());
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);
add(op1);
add(op2);
add(op3);
add(op4);
add(op5);
add(out);
b1.addActionListener(new BActionAdapter());
b2.addActionListener(new BActionAdapter());
b3.addActionListener(new BActionAdapter());
b4.addActionListener(new BActionAdapter());
b5.addActionListener(new BActionAdapter());
b6.addActionListener(new BActionAdapter());
b7.addActionListener(new BActionAdapter());
b8.addActionListener(new BActionAdapter());
b9.addActionListener(new BActionAdapter());
b0.addActionListener(new BActionAdapter());
op1.addActionListener(new BActionAdapter());
op2.addActionListener(new BActionAdapter());
op3.addActionListener(new BActionAdapter());
op4.addActionListener(new BActionAdapter());
op5.addActionListener(new BActionAdapter());
setSize(300, 300);
show();
}
String op;
double a;
double b;
class BActionAdapter implements ActionListener {
boolean t;
public void actionPerformed(ActionEvent e) {
String click = e.getActionCommand();
out.setText(out.getText() + click);
if (!click.equals("+") && !click.equals("-") && !click.equals("*") && !click.equals("/") && !click.equals("=")) {
if (s1 == null)// 判断是否是第一个数
s1 = click;
else
s1 += click;
a = new Double(s1).doubleValue();
}
else if (!click.equals("=")) {
if (click.equals("+") || !click.equals("-") || !click.equals("*") || !click.equals("/")) {
op = click;
s1 = null;
}
b = a;
t = true;
}
else {
if (op != null) {
if (op.equals("+"))
a = a + b;
else if (op.equals("-"))
a = b - a;
else if (op.equals("*"))
a = a * b;
else if (op.equals("/"))
a = b / a;
}
out.setText(out.getText() + a);
t = false;
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询