| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 456 人关注过本帖
标题:计算器 算法出错了 帮忙看看~
取消只看楼主 加入收藏
zz8386
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-10-4
收藏
 问题点数:0 回复次数:0 
计算器 算法出错了 帮忙看看~

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Toolkit;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Calculators extends JFrame implements ActionListener{
int screenWidth=Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight=Toolkit.getDefaultToolkit().getScreenSize().height;
int frameWidth=310;
int frameHeight=240;
Point p=new Point((screenWidth-frameHeight)/2,(screenHeight-frameHeight)/2);

private double num=0.0;
private String operator="=";
private boolean firstDigit=true;
private boolean operatorValidFlag=true;
private final String[] btnTexts={
"7","8","9","/","sqrt","4","5","6","*","1/x","1","2",
"3","-","%","0",".","+/-","+","="};
private static JTextField enterNum=new JTextField("0");
private JButton backSButton=new JButton("backsplace");
private JButton clearButton=new JButton("clear");
private JButton[] keys=new JButton[btnTexts.length];
public Calculators(){
super();
enterNum.setHorizontalAlignment(JTextField.RIGHT);
enterNum.setEditable(false);
enterNum.setBackground(Color.white);
Container cp=this.getContentPane();
JPanel p1=new JPanel(new GridLayout(1,2));
p1.add(backSButton);
p1.add(clearButton);
JPanel p2=new JPanel(new GridLayout(2,1));
p2.add(enterNum);
p2.add(p1);
JPanel p3=new JPanel(new GridLayout(4,5,3,3));
for(int i=0;i<btnTexts.length;i++){
keys[i]=new JButton(btnTexts[i]);
keys[i].addActionListener(this);
p3.add(keys[i]);
keys[i].setForeground(Color.blue);
}
keys[3].setForeground(Color.red);
keys[8].setForeground(Color.red);
keys[13].setForeground(Color.red);
keys[18].setForeground(Color.red);
keys[19].setForeground(Color.red);
cp.add(p2,BorderLayout.NORTH);
cp.add(p3,BorderLayout.CENTER);
this.pack();

backSButton.addActionListener(this);
clearButton.addActionListener(this);
this.setSize(frameWidth,frameHeight);
this.setLocation(p);
this.setTitle("Calculator");
this.setResizable(false);
this.setVisible(true);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
dispose();
}
});
}
public void actionPerformed(ActionEvent ae){
String label=ae.getActionCommand();
if(label.equals(backSButton)){
handleBackSplace();
}
else if(label.equals(clearButton)){
enterNum.setText("0");
}
else if("0123456789.".indexOf(label)>=0){

handleNumber(label);
}
else {
handleOperator(label);
}
}
private void handleBackSplace(){
String str=enterNum.getText();
int i=str.length();
if(i>0){
str=str.substring(0,i-1);

if(str.length()==0){
enterNum.setText("0");
firstDigit=true;
operator="=";
}
else{
enterNum.setText(str) ;
}
}
}
private void handleNumber(String key){
if(firstDigit){
enterNum.setText(key);
}
else if(key.equals(".")&&(enterNum.getText().indexOf(".")<0)){
enterNum.setText(enterNum.getText()+key);

}
else if(!key.equals(".")){
enterNum.setText(enterNum.getText()+key);
}
firstDigit=false;
}
private void handleOperator(String key){
if(key.equals("/")){
if(getNumberFromText()==0.0){
operatorValidFlag=false;
enterNum.setText("dividend nunber can not be zero");
}
else{
num/=getNumberFromText();
}
}
else if(key.equals("1/x")){
if(num==0.0){
operatorValidFlag=false;
enterNum.setText("zero has no reciprocal");
}
else{
num=1/num;
}
}
else if(key.equals("+/-")){
num=(-1)*num;
}
else if(key.equals("+")){
num+=getNumberFromText();
}
else if(key.equals("-")){
num-=getNumberFromText();
}
else if(key.equals("*")){
num*=getNumberFromText();
}
else if(key.equals("=")){
num=getNumberFromText();
}
else if(key.equals("sqrt")){
num=Math.sqrt(num);
}
if(operatorValidFlag){
long t1;
double t2;
t1 = (long) num;
t2 = num - t1;
if (t2 == 0) {
enterNum.setText(String.valueOf(t1));
} else {
enterNum.setText(String.valueOf(num));
}

}
operator=key;
firstDigit=true;
operatorValidFlag=true;
}
private double getNumberFromText(){
double number=0.0;
try{
number=Double.valueOf(enterNum.getText()).doubleValue();
}
catch(NumberFormatException e){
e.printStackTrace();
}
return number;
}

public static void main(String[] args) {
Calculators cal=new Calculators();

}
}

搜索更多相关主题的帖子: 计算器 算法 
2007-04-06 20:25
快速回复:计算器 算法出错了 帮忙看看~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040803 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved