初学JAVA三个月,编了一个中文计算机程序,觉得好的顶一下

时间:2022-12-28 21:08:03
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame {
boolean operaPressed = false;
boolean numberPressed = true;
boolean equalPressed = false;
int singleNumber;
String  displayString = "";
int dotFactorial;
char operator;
boolean dotPressed = false;
double  tempNumber = 0.0;   
double  numAfterOperPressed;
double numberCalculated;
double numberStored;   // store the number wihc maybe used later
private JButton sqrt;
private JButton divide;
private JButton  MEM; // store the number which may be used later
private JButton  GEM; // get the number which has been stroed
private JButton  Clear;   // reset the caculator,
private JButton  seven7;
private JButton  eight8;
private JButton  nine9;
private JButton  plus;
private JButton  four4;
private JButton  five5;
private JButton  six6;
private JButton  multiply;
private JButton  one1;
private JButton  two2;
private JButton  three3;
private JButton  minus;
private JButton  zero0;
private JButton  dot;
private JButton  equals;
private JButton  plus1;
private JPanel   topPanel;
private JPanel   backPanel;
private JTextField display;
protected Calculator calculator;
public Calculator()
{
topPanel = new JPanel();
topPanel.setLayout(new GridLayout(5,4));
sqrt = new JButton("sqrt");
topPanel.add(sqrt);
MEM = new JButton("MEM");
topPanel.add(MEM);
GEM = new JButton("GEM");
topPanel.add(GEM);
Clear = new JButton("C");
topPanel.add(Clear);
seven7 = new JButton("7");
topPanel.add(seven7);
eight8 = new JButton("8");
topPanel.add(eight8);
nine9 = new JButton("9");
topPanel.add(nine9);
plus = new JButton ("+");
topPanel.add(plus);
four4 = new JButton("4");
topPanel.add(four4);
five5 = new JButton("5");
topPanel.add(five5);
six6 = new JButton ("6");
topPanel.add(six6);
multiply = new JButton("*");
topPanel.add(multiply);
one1 = new JButton("1");
topPanel.add(one1);
two2 = new JButton("2");
topPanel.add(two2);
three3 = new JButton("3");
topPanel.add(three3);
minus = new JButton("-");
topPanel.add(minus);
zero0 = new JButton("0");
topPanel.add(zero0);
dot = new JButton(".");
topPanel.add(dot);
equals = new JButton("=");
topPanel.add(equals);
divide = new JButton("/");
topPanel.add(divide);
display = new JTextField(20);
backPanel = new JPanel();
backPanel.setLayout(new BorderLayout());
backPanel.setBackground(Color.red);
backPanel.add(display,BorderLayout.NORTH);
backPanel.add(topPanel,BorderLayout.CENTER);
getContentPane().add(backPanel);
setTitle("Calculator");
setSize(300,300);
setLocation(400,400);
pack();
setVisible(true);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
nine9.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(9);
}

});

eight8.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(8);
}

}); 
seven7.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(7);
}

});

28 个解决方案

#1


six6.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(6);
}

});
five5.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(5);
}

});
four4.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(4);
}

});
three3.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(3);
}

});
two2.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(2);
}

});
one1.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(1);
}

});
zero0.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(0);
}

});
Clear.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressClear();
}

});
dot.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
dotPressed = true;
displayString += '.';
display.setText(displayString);
}
});
plus.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
display.setText("加上");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else 
tempNumber = numberCalculated;
operator = '+' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
});
minus.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
display.setText("减去");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else 
tempNumber = numberCalculated;
operator = '-' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
});
multiply.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
display.setText("乘以");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else 
tempNumber = numberCalculated;
operator = '*' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
});
divide.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
display.setText("除以");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else 
tempNumber = numberCalculated;
operator = '/' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
});
sqrt.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{

display.setText("开方");
if(operaPressed)
calculatAction();
if(!operaPressed && !equalPressed)
    numberCalculated = tempNumber ;
operator = 's' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
});
equals.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
calculatAction();
display.setText(""+numberCalculated);
equalPressed = true;
numberPressed = false;
operaPressed = false;
    tempNumber = 0.0;
    dotFactorial = 0;
}
});
MEM.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
    display.setText("保存这个数");
numberStored = numberCalculated;
 operaPressed = false;
 numberPressed = true;
 equalPressed = false;
 displayString = " ";
 operator = ' ';
 dotPressed = false;
 tempNumber = 0.0;
 numberCalculated = 0.0;
 numAfterOperPressed = 0.0;
 dotFactorial = 0;
}
});
GEM.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
    display.setText("取出保存的数");
tempNumber = numberStored;

}
});
}
public void calculatAction()
{
switch(operator)
{
    case '+' : 
        numberCalculated = numberCalculated + tempNumber;
        break;
    case '-' :
     numberCalculated = numberCalculated - tempNumber;
     break;
    case '*' :
     numberCalculated = numberCalculated * tempNumber;
     break;
    case '/' :
     numberCalculated = numberCalculated / tempNumber;
     break;
    case 's' :
     numberCalculated = Math.sqrt(numberCalculated);
        }
}
void actionAfterPressNum (int i)
{
if(numberPressed)
{
displayString += i;
display.setText(displayString);
}
else 
{
displayString = Integer.toString(i);
display.setText("" + i);
}
if (dotPressed && numberPressed)
{
tempNumber += i*Math.pow(10,-(++dotFactorial));
}
else 
tempNumber = tempNumber*10 + i;
numberPressed = true;
}
void actionAfterPressClear()
{
 operaPressed = false;
 numberPressed = true;
 equalPressed = false;
 displayString = " ";
 display.setText("0");
 operator = ' ';
 dotPressed = false;
 tempNumber = 0.0;
 numberCalculated = 0.0;
 numAfterOperPressed = 0.0;
 dotFactorial = 0;
}

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

}

#2


帮你顶,

#3


mark

#4


up

#5


#6


UP

#7


不要光顶啊,运行看看,有什么问题

#8


mark

#9


你写程序不写注释,
会害人又害己的哦。

#10


#11


真长

#12


真长

#13


裹脚布吗?这么长。

#14


实现的东西多啊,其实就是定义JButton比较长,算法简单

#15



#16


不错啊:)     
 如果能结合 数据结构中的 表达式求值 更好一点。

#17


呵呵 你少了 清零的按扭了。
  我也刚学java...

#18


回复人: aheadyes() ( ) 信誉:100  2004-11-05 11:25:00  得分: 0  
 
 
   呵呵 你少了 清零的按扭了。
  我也刚学java...
===============================================
"C"就是清零键噻  
 

#19


呵呵
接分

#20


up

#21


up

#22


“中文计算机程序”!?
 
楼主用计算器编程!?

#23




是计算器吧???

#24


好长中啊

#25


“中文计算机程序”!?
 
楼主用计算器编程!?

#26


哈哈

#27


是计算器吧。

#28


参看我的
http://community.csdn.net/Expert/topic/3547/3547115.xml?temp=.7781031

#1


six6.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(6);
}

});
five5.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(5);
}

});
four4.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(4);
}

});
three3.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(3);
}

});
two2.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(2);
}

});
one1.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(1);
}

});
zero0.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressNum(0);
}

});
Clear.addActionListener(new ActionListener()
{   
public void actionPerformed (ActionEvent e)
{
actionAfterPressClear();
}

});
dot.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
dotPressed = true;
displayString += '.';
display.setText(displayString);
}
});
plus.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
display.setText("加上");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else 
tempNumber = numberCalculated;
operator = '+' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
});
minus.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
display.setText("减去");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else 
tempNumber = numberCalculated;
operator = '-' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
});
multiply.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
display.setText("乘以");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else 
tempNumber = numberCalculated;
operator = '*' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
});
divide.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
display.setText("除以");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else 
tempNumber = numberCalculated;
operator = '/' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
});
sqrt.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{

display.setText("开方");
if(operaPressed)
calculatAction();
if(!operaPressed && !equalPressed)
    numberCalculated = tempNumber ;
operator = 's' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
});
equals.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
calculatAction();
display.setText(""+numberCalculated);
equalPressed = true;
numberPressed = false;
operaPressed = false;
    tempNumber = 0.0;
    dotFactorial = 0;
}
});
MEM.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
    display.setText("保存这个数");
numberStored = numberCalculated;
 operaPressed = false;
 numberPressed = true;
 equalPressed = false;
 displayString = " ";
 operator = ' ';
 dotPressed = false;
 tempNumber = 0.0;
 numberCalculated = 0.0;
 numAfterOperPressed = 0.0;
 dotFactorial = 0;
}
});
GEM.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
    display.setText("取出保存的数");
tempNumber = numberStored;

}
});
}
public void calculatAction()
{
switch(operator)
{
    case '+' : 
        numberCalculated = numberCalculated + tempNumber;
        break;
    case '-' :
     numberCalculated = numberCalculated - tempNumber;
     break;
    case '*' :
     numberCalculated = numberCalculated * tempNumber;
     break;
    case '/' :
     numberCalculated = numberCalculated / tempNumber;
     break;
    case 's' :
     numberCalculated = Math.sqrt(numberCalculated);
        }
}
void actionAfterPressNum (int i)
{
if(numberPressed)
{
displayString += i;
display.setText(displayString);
}
else 
{
displayString = Integer.toString(i);
display.setText("" + i);
}
if (dotPressed && numberPressed)
{
tempNumber += i*Math.pow(10,-(++dotFactorial));
}
else 
tempNumber = tempNumber*10 + i;
numberPressed = true;
}
void actionAfterPressClear()
{
 operaPressed = false;
 numberPressed = true;
 equalPressed = false;
 displayString = " ";
 display.setText("0");
 operator = ' ';
 dotPressed = false;
 tempNumber = 0.0;
 numberCalculated = 0.0;
 numAfterOperPressed = 0.0;
 dotFactorial = 0;
}

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

}

#2


帮你顶,

#3


mark

#4


up

#5


#6


UP

#7


不要光顶啊,运行看看,有什么问题

#8


mark

#9


你写程序不写注释,
会害人又害己的哦。

#10


#11


真长

#12


真长

#13


裹脚布吗?这么长。

#14


实现的东西多啊,其实就是定义JButton比较长,算法简单

#15



#16


不错啊:)     
 如果能结合 数据结构中的 表达式求值 更好一点。

#17


呵呵 你少了 清零的按扭了。
  我也刚学java...

#18


回复人: aheadyes() ( ) 信誉:100  2004-11-05 11:25:00  得分: 0  
 
 
   呵呵 你少了 清零的按扭了。
  我也刚学java...
===============================================
"C"就是清零键噻  
 

#19


呵呵
接分

#20


up

#21


up

#22


“中文计算机程序”!?
 
楼主用计算器编程!?

#23




是计算器吧???

#24


好长中啊

#25


“中文计算机程序”!?
 
楼主用计算器编程!?

#26


哈哈

#27


是计算器吧。

#28


参看我的
http://community.csdn.net/Expert/topic/3547/3547115.xml?temp=.7781031