求解答-运行结果出现的问题我不知道怎么改,请会的你们帮忙一下吧,很急!

问题描述

运行结果出现的问题我不知道怎么改,请会的你们帮忙一下吧,很急!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class UserGUI implements ActionListener
{ private CalcEngine calc;
private JFrame frame;
private JTextField display;
private JLabel status;
public UserGUI(CalcEngine engine)
{ calc=engine;
makeFrame();
frame.setVisible(true); }
public void setVisible(boolean visible)
{ frame.setVisible(visible); }
private void makeFrame()
{ setBackground(Color.lightGray);

setForeground(Color.blue);
frame=new JFrame(calc.getTitle());
JPanel contentPane=(JPanel)frame.getContentPane();
contentPane.setLayout(new BorderLayout(8,8));
contentPane.setBorder(new EmptyBorder(10,10,10,10));
display=new JTextField();
contentPane.add(display,BorderLayout.NORTH);
JPanel buttonPanel =new JPanel(new GridLayout(0,5));
Button[] b=new Button[10];
for(int i=0;i<=9;i++)

b[i]=new Button(Integer.toString(i));
addButton(buttonPanel,"7");
addButton(buttonPanel,"8");
addButton(buttonPanel,"9");
addButton(buttonPanel,"+");
addButton(buttonPanel,"4");
addButton(buttonPanel,"5");
addButton(buttonPanel,"6");
addButton(buttonPanel,"-");
addButton(buttonPanel,"1");
addButton(buttonPanel,"2");
addButton(buttonPanel,"3");
addButton(buttonPanel,"*");
addButton(buttonPanel,"0");
addButton(buttonPanel,".");
addButton(buttonPanel,"C");
addButton(buttonPanel,"/");
addButton(buttonPanel,"=");
addButton(buttonPanel,"sqrt");
addButton(buttonPanel,"%");
addButton(buttonPanel,"sin");
addButton(buttonPanel,"tan");
contentPane.add(buttonPanel,BorderLayout.CENTER);
frame.pack();
}

private void addButton(Container panel,String buttonText)
{ JButton button=new JButton(buttonText);
button.addActionListener(this);
panel.add(button); }
public void actionPerformed(ActionEvent event)
{ String command=event.getActionCommand();
for(i=0;i<=9;i++)
if(command.equals(b[i]))||command.equals("."))
{ number1=String.parseString(command);
calc.numberPressed(number1); }
else if(command.equals("+")) calc.plus();
else if(command.equals("-")) calc.minus();
else if(command.equals("*")) calc.mul();
else if(command.equals("/")) calc.div();
else if(command.equals("sqrt")) calc.genhao();
else if(command.equals("%")) calc.quyu();
else if(command.equals("C")) calc.clear();
else if(command.equals("=")) calc.equals();
else if(command.equals("sin")) calc.sinzhi();
else if(command.equals("tan")) calc.tanzhi();
redisplay();
}
private void redisplay()
{ display.setText(""+calc.getDisplayValue()); }
}

class Calculator
{ private CalcEngine engine;
private UserGUI gui;
public Button point;
public Calculator()
{ engine=new CalcEngine();
gui=new UserGUI(engine); }

public void show()
{ gui.setVisible(true); }
public static void main(String a[])
{ Calculator calc=new Calculator();
calc.show(); }
}
class CalcEngine
{ public String number1,number2;
Double result;
final double pi=3.1415926535;
char flag;
boolean isNumber;
point = new Button(".");
public CalcEngine()
{ number1=' ';
number2=' ';
flag=' ';
isNumber=true;
}
public void doshuru(String s)

{ number1 += s; UserGUI.contentPane.setText(number1);}
public void doshuru(String s)

{ number2 += s; UserGUI.contentPane.setText(number2);}
public double getDisplayValue()
{ return result; }

public boolean action(Event e, Object o)

{ String s = new String("");
for(int i=0;i<10;i++)
{ if(e.target==b[i]||e.target==point)

{ if(e.target != point)

{ s = (String)o;
doshuru(s); }
if(e.target == point)

{ s = (String)o;
if(number1.equals(""))
number1 += "0.";

else
dodoshuru(s);

}
}
}
}

public void numberPressed( )
{ if(isNumber&&this.number1!=0)
{ double str1=(new Double(number1)).doubleValue();
double str2=(new Double(number2)).doubleValue();
double this.number1=Double.parseDouble(str2+str1);
}
else
{ this.number1=number1; }
isNumber=true;
}
public void plus()
{ equals(); this.flag='+'; }
public void minus()
{ equals(); this.flag='-'; }
public void mul()
{ equals(); this.flag='*'; }
public void div()
{ equals(); this.flag='/'; }
public void genhao()
{ equals(); this.flag='√'; }
public void quyu()
{ equals(); this.flag='%'; }
public void sinzhi()
{ equals(); this.flag='s'; }
public void tanzhi()
{ equals(); this.flag='t'; }
public double equals()
{ switch(flag)
{ case '+': result=new Double((str1 + str2)); break;
case '-': result=new Double((str1 - str2)); break;
case '*': result=new Double((str1 * str2)); break;
case '/': result=new Double((str1 / str2)); break;
case '√': result=new Double((sqrt(str1))); break;
case 't': result=new Double((Math.tan(str1*pi/180))); break;
case 's': result=new Double((Math.sin(str1*pi/180))); break;
case '%': result=new Double((str1 % str2)); break;

case ' ': result= str1; break;
}
str1=result;
flag=' ';
isNumber=false;
}
public void clear()
{ number=0; result=0; }
public String getTitle()
{ return " calclation example"; }
}

时间: 2024-11-04 15:30:09

求解答-运行结果出现的问题我不知道怎么改,请会的你们帮忙一下吧,很急!的相关文章

Anroid 求解答代码中如何获取到抽象类的对象,从而调用抽象类中的方法。(急)

问题描述 Anroid 求解答代码中如何获取到抽象类的对象,从而调用抽象类中的方法.(急) public abstract class DataSource { protected static DataSource me; public static DataSource getInstance() { if (me == null) {} try { if (me == null) { //下面这一行代码在Application中是空实现 MyApplication.getInstance(

c语言-sql存储函数求助。求大神帮忙!!很急

问题描述 sql存储函数求助.求大神帮忙!!很急 现需要在sql数据库表C中添加一标志列,并根据标志列另一字端活动编号是等于A表的活动编号,还是等于B表活动编号分别赋为1或0.整个sql如何写 解决方案 SELECTC.* CASE WHEN A.活动编号 IS NOT NULL THEN 1 WHEN B.活动编号 IS NOT NULL THEN 0 ENDFROM CLEFT OUTER JOIN AON C.活动编号 = A.活动编号LEFT OUTER JOIN BON C.活动编号

path-spring-mabatis.xml报错不知道什么原因请各位大神帮忙解答

问题描述 spring-mabatis.xml报错不知道什么原因请各位大神帮忙解答 Context initialization failed.....class path resource [spring-mybatis.xml] cannot be opened because it does not exist 解决方案 解决方案二: 解决方案三: 前面的回答被吃了,白回答了一次..... 解决方案四: spring-mybatis.xml文件不在classpath路径中 如果spring

在eclipse 下面,为什么编写的C/S架构的软件编译后无法运行,是不是eclipse在编译C/S架构软件时需要一个插件呢?很急!!!!请教!!!

问题描述 在eclipse下面,为什么编写的C/S架构的软件编译后无法运行,是不是eclipse在编译C/S架构软件时需要一个插件呢?很急!!!!请教!!!我在eclipse下面下面写了一个完成一个小功能的C/S架构的软件,如果在jbuilder下编写和编译后,会生成.exe文件,而且可以独立运行,但是我现在在eclipse下面下面写了一个完成一个小功能的C/S架构的软件,但是不能编译成所要的.exe文件,请教高手,怎样解决??????????? 解决方案 解决方案二:eclipse编译不出来E

求解答-运行结果正确,在连续5黑或5白可以自动出现一个对话框,显示赢了也可选继续或退出游戏

问题描述 运行结果正确,在连续5黑或5白可以自动出现一个对话框,显示赢了也可选继续或退出游戏 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; import java.awt.*; public class Examp

求解答-运行结果怎么回事,dev c++运行从74开始,求解

问题描述 运行结果怎么回事,dev c++运行从74开始,求解 #include #define MAX 100 int main(void) { int count = MAX + 1; while(--count>0){ printf("%d bottles of spring water on the wall," "%d bottles of spring water!n",count,count); printf("Take one dow

求解答-运行结果说是Exception in thread &amp;amp;quot;main&amp;amp;quot; java.NoClassDeFoundError:Example

问题描述 运行结果说是Exception in thread "main" java.NoClassDeFoundError:Example import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; import j

文件-c语言 火车票管理系统 求解答

问题描述 c语言 火车票管理系统 求解答 运行后不能添加,删除,修改,查询班次到文件 可是文件是已经建好有数据的,查询时只显示一条然后就非正常退出了,我怀疑是最后链表做的有问题函数read()可是就是找不到哪错了,希望有哪位朋友可以解答一下, #include #include #include #include #include #include typedef struct Tickets { char num; char time[8]; char starts[8]; char ends

java-初学Hibernate遇到问题,求解答~

问题描述 初学Hibernate遇到问题,求解答~ 运行一个hibernate例子,但是报错,小白不知道该怎么解决,麻烦大神帮看看.我把代码贴出来~(1).package hibernate;import org.hibernate.*;import org.hibernate.cfg.*;import org.hibernate.service.*;import org.hibernate.boot.registry.*;public class NewsManager{ public sta