用java编写计算器用了什么技术(能够用Java编写一个计算器是什么水平了)
本篇文章给大家谈谈用java编写计算器用了什么技术,以及能够用Java编写一个计算器是什么水平了对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。
JAVA 编写计算器 要代码最简单的
学java的时候自己编的,很简单,能够连续输入计算式后进行计算
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**简易计算器,能够进行简单的计算
*
*@see 2008.12.9
*/
public class CalculatorA
implements ActionListener{
private JFrame frame;
private JTextField field;
private JButton[] allButtons;
private JButton clearButton;
// private JButton backButton;
String result="";//保存结果
StringBuilder sb= new StringBuilder();//保存要进行的计算式
int x= 0;//用来判断上一次的事件类型
String str="123+456-789*0.=/";
ArrayList<String> arrayList= new ArrayList<String>();//保存计算式,通过方法进行运算
public CalculatorA(){
frame= new JFrame("我的计算器v1.1");
frame.setLocation(300,300);
field= new JTextField(25);
allButtons= new JButton[16];
for(int i=0;i<allButtons.length;i++){
allButtons[i]= new JButton(str.substring(i,i+1));
}
clearButton= new JButton("CLEAR");
// backButton= new JButton("<——");
init();
setFondAndColor();
addEventHander();
}
public void init(){
frame.setLayout(new BorderLayout());
JPanel northPanel= new JPanel();
JPanel centerPanel= new JPanel();
JPanel southPanel= new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(field);
for(int i=0;i<allButtons.length;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
//southPanel.add(backButton);
frame.add(northPanel,BorderLayout.NORTH);
frame.add(centerPanel,BorderLayout.CENTER);
frame.add(southPanel,BorderLayout.SOUTH);
}
//设置输入字体
public void setFondAndColor(){
field.setFont(new Font("宋体",Font.BOLD,24));
field.setBackground(new Color(100,200,200));
field.setForeground(Color.RED);
//设置字体从右起始
field.setHorizontalAlignment(JTextField.RIGHT);
}
public void showMi(){
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addEventHander(){
for(int i=0;i<allButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(this);
// backButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e){
String str= e.getActionCommand();//取得当前事件返回的值
if("0123456789.".indexOf(str)!=-1){
if(x== 0){//当x为0时表示还没有进行输入
result=str;
sb.append(str);
field.setText(str);
x= 1;
}
else if(x== 1){
result= result+str;
sb.append(str);
field.setText(result);
x= 1;
}
else if(x== 2){
sb.delete(0,sb.length());
result= result+str;
sb.append(str);
field.setText(result);
x= 1;
}
else if(x== 3){
result= str;
sb.delete(0,sb.length());
arrayList.clear();
field.setText(str);
sb.append(str);
field.setText(str);
x= 1;
}
else if(x== 4){
result="";
sb.delete(0,sb.length());
arrayList.clear();
result= str;
sb.append(str);
field.setText(str);
x= 1;
}
else{
result= result+str;
sb.append(str);
field.setText(result);
x= 1;
}
}
else if("+*-/".indexOf(str)!=-1){
if(x== 0){
field.setText("");
x= 2;
}
else if(x== 1){
result= result+ str;
arrayList.add(sb.toString());
arrayList.add(str);
sb.append(str);
field.setText(result);
x= 2;
}
else if(x== 2){
x= 2;
}
else if(x== 3){
field.setText(result+str);
arrayList.add(result);
arrayList.add(str);
result= result+str;
x= 2;
}
else if(x== 4){
result="";
sb.delete(0,sb.length());
arrayList.clear();
x= 2;
}
else{
field.setText(result+str);
arrayList.add(result);
arrayList.add(str);
result= result+str;
x= 2;
}
}
else if("=".equals(str)){
if(x== 0){
field.setText("0");
arrayList.clear();
result="0";
x= 3;
}
else if(x== 1){
try{
arrayList.add(sb.toString());
arrayList= getResult(arrayList);
result= arrayList.get(0);
field.setText(result);
arrayList.clear();
x= 3;
}catch(Exception e1){
field.setText("数据格式异常");
x= 0;
}
}
else if(x== 2){
field.setText("数据格式错误.....");
arrayList.clear();
x= 0;
}
else if(x== 3){
field.setText(result);
x= 3;
}
else if(x== 4){
result="";
sb.delete(0,sb.length());
arrayList.clear();
x= 3;
}
else{
try{
arrayList.add(sb.toString());
arrayList= getResult(arrayList);
result= arrayList.get(0);
field.setText(result);
arrayList.clear();
x= 3;
}catch(Exception e1){
field.setText("数据格式异常");
x= 0;
}
}
}
else if("CLEAR".equals(str)){
arrayList.clear();
field.setText("0");
arrayList.add("0");
x= 4;
}
else{
if(result.length()>1){
result= result.substring(0,result.length()-1);
if(sb.length()>0){
sb.delete(sb.length()-1,sb.length());
}
else{
sb.delete(0,1);
}
field.setText(result);
x= 5;
}
else{
result="";
sb.delete(0,sb.length());
arrayList.clear();
field.setText("0");
x= 0;
}
}
}
public static ArrayList<String> getResult(ArrayList<String> list){
String res= null;
String[] s={"/","*","-","+"};
int i=0;
if(list.size()>1){
for(;i<s.length;){
if(s[i].equals("/")){
for(int j=0;j<list.size();j++){
if(list.get(j).equals(s[i])){
res= Double.toString(Double.parseDouble(list.get(j-1))/Double.parseDouble(list.get(j+1)));
//本地的数据格式
NumberFormat nf= NumberFormat.getInstance();
res= nf.format(Double.parseDouble(res));
res= getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else if(s[i].equals("*")){
for(int j=0;j<list.size();j++){
if(list.get(j).equals(s[i])){
res= Double.toString(Double.parseDouble(list.get(j-1))*Double.parseDouble(list.get(j+1)));
NumberFormat nf= NumberFormat.getInstance();
res= nf.format(Double.parseDouble(res));
res= getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else if(s[i].equals("-")){
for(int j=0;j<list.size();j++){
if(list.get(j).equals(s[i])){
res= Double.toString(Double.parseDouble(list.get(j-1))-Double.parseDouble(list.get(j+1)));
NumberFormat nf= NumberFormat.getNumberInstance();
res= nf.format(Double.parseDouble(res));
res= getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else{
for(int j=0;j<list.size();j++){
if(list.get(j).equals(s[i])){
res= Double.toString(Double.parseDouble(list.get(j-1))+Double.parseDouble(list.get(j+1)));
NumberFormat nf= NumberFormat.getInstance();
res= nf.format(Double.parseDouble(res));
res= getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
}
}
return list;
}
//对数字字符串进行排除不必要符号
public static String getChange(String res){
String s_temp="";
char[] c= new char[res.length()];
for(int k=0;k<c.length;k++){
c[k]= res.charAt(k);
}
for(int k=0;k<c.length;k++){
if((c[k]>='0'&& c[k]<='9')|| c[k]=='.'){
s_temp+= c[k];
}
}
return s_temp;
}
public static void main(String[] args){
new CalculatorA().showMi();
}
}
求"用JAVA编写的计算器程序代码"
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class jsq
{
public static void main(String[] str)
{
jisuanqi jsq=new jisuanqi();
jsq.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jsq.setVisible(true);jsq.setSize(300,300);
}
}
class jisuanqi extends JFrame implements ActionListener
{
double a=0,b=0,c=0,fuhao=5;
Boolean has=false;
Container A;
JTextField tf1;
JButton bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0,bt_dian;
JButton bt_jia,bt_jian,bt_cheng,bt_chu,bt_fuhao;
JButton bt_clear,bt_jisuan;
public jisuanqi()
{
setTitle("简单计算器");
A=getContentPane();
GridBagLayout f=new GridBagLayout();
A.setLayout(f);
GridBagConstraints g=new GridBagConstraints();
g.fill=GridBagConstraints.BOTH;
g.gridwidth=1;
g.gridheight=1;
//面板的实例化
//显示框的实例化
tf1=new JTextField(10);tf1.setEditable(false);
//数字键的实例化
bt1=new JButton("1");bt2=new JButton("2");bt3=new JButton("3");
bt4=new JButton("4");bt5=new JButton("5");bt6=new JButton("6");
bt7=new JButton("7");bt8=new JButton("8");bt9=new JButton("9");
bt0=new JButton("0");bt_dian=new JButton(".");bt_fuhao=new JButton("+/-");
//计算符号的实例化
bt_jia=new JButton("+");bt_jian=new JButton("-");
bt_cheng=new JButton("*");bt_chu=new JButton("/");
//清空和计算按钮的实例化
bt_clear=new JButton("clear");bt_jisuan=new JButton("=");
//布局
g.gridx=1;g.gridy=0;A.add(tf1,g);
g.gridx=0;g.gridy=1;A.add(bt_clear,g);
g.gridx=3;g.gridy=1;A.add(bt_jisuan,g);
g.gridx=0;g.gridy=2;A.add(bt7,g);
g.gridx=1;g.gridy=2;A.add(bt8,g);
g.gridx=2;g.gridy=2;A.add(bt9,g);
g.gridx=3;g.gridy=2;A.add(bt_jia,g);
g.gridx=0;g.gridy=3;A.add(bt4,g);
g.gridx=1;g.gridy=3;A.add(bt5,g);
g.gridx=2;g.gridy=3;A.add(bt6,g);
g.gridx=3;g.gridy=3;A.add(bt_jian,g);
g.gridx=0;g.gridy=4;A.add(bt1,g);
g.gridx=1;g.gridy=4;A.add(bt2,g);
g.gridx=2;g.gridy=4;A.add(bt3,g);
g.gridx=3;g.gridy=4;A.add(bt_cheng,g);
g.gridx=0;g.gridy=5;A.add(bt0,g);
g.gridx=1;g.gridy=5;A.add(bt_fuhao,g);
g.gridx=2;g.gridy=5;A.add(bt_dian,g);
g.gridx=3;g.gridy=5;A.add(bt_chu,g);
//添加监听
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
bt0.addActionListener(this);
//清除、小数点、符号添加监听
bt_clear.addActionListener(this);
bt_dian.addActionListener(this);
bt_fuhao.addActionListener(this);
//符号添加监听
bt_jia.addActionListener(this);
bt_jian.addActionListener(this);
bt_cheng.addActionListener(this);
bt_chu.addActionListener(this);
//计算符号添加监听
bt_jisuan.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//数字键
if(e.getSource()==bt1)
{tf1.setText(tf1.getText()+"1");}
else if(e.getSource()==bt2)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"2");
}
else if(e.getSource()==bt3)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"3");
}
else if(e.getSource()==bt4)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"4");
}
else if(e.getSource()==bt5)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"5");
}
else if(e.getSource()==bt6)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"6");
}
else if(e.getSource()==bt7)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"7");
}
else if(e.getSource()==bt8)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"8");
}
else if(e.getSource()==bt9)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"9");
}
else if(e.getSource()==bt0)
{
if(!tf1.getText().equals("0"))
{
tf1.setText(tf1.getText()+"0");
}
}
else if(e.getSource()==bt_dian)//小数点符号
{
if(tf1.getText().indexOf(".")==-1&&tf1.getText().length()>0)
{
tf1.setText(tf1.getText()+".");
}
}
//运算符号
//fuhao的0,1,2,3分别表示加、减、乘、除
else if(e.getSource()==bt_jia)
{
if(tf1.getText().trim().length()>0)
{ a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=0;
has=true;
}
}
else if(e.getSource()==bt_jian)
{
if(tf1.getText().trim().length()>0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=1;
has=true;
}
}
else if(e.getSource()==bt_cheng)
{
if(tf1.getText().trim().length()>0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=2;
has=true;
}
}
else if(e.getSource()==bt_chu)
{
if(tf1.getText().trim().length()>0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=3;
has=true;
}
}
//计算、符号、清空
else if(e.getSource()==bt_jisuan)//计算结果
{
try
{
if(tf1.getText().length()>0&&has)
{
b=Double.valueOf(tf1.getText());
if(fuhao==0)
{
c=a+b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==1)
{
c=a-b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==2)
{
c=a*b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==3)
{
c=a/b;
tf1.setText(String.valueOf(c));
has=false;
}
}
}
catch(Exception ex)
{tf1.setText(ex.getMessage().toString());}
}
else if(e.getSource()==bt_fuhao)
{
if(tf1.getText().indexOf("-")==-1&&tf1.getText().length()>0)
{
String s=tf1.getText();
tf1.setText("-"+s);
}
else
{
if(tf1.getText().length()>0)
{
String s=tf1.getText().substring(1);
tf1.setText(s);
}
}
}
else if(e.getSource()==bt_clear)
{
tf1.setText("");
}
}
}
能够用Java编写一个计算器是什么水平了
水平应该说不错,如果表达式解析算法是自己实现的,而没有使用现成的类库。当然如果使用现成类库的话,稍微懂一点java的人都能写出来。
我个人认为一个计算器的编写还是不太容易的,我已经工作一年多了,但是如果让我现在写一个计算器,一时半活儿还真写不出来。
我觉得计算器的算法实现还是有点复杂的,如果没有系学过数据结构,一般情况下是写不出来的(不知道有没有特例)。
一个计算器的实现,主要是算法方面的要求(当然已经存在成熟算法了),一个人即使ejb,spring,hibernate,html等知识懂的再多,也未必真的写出一个计算器。像ejb,spring,hibernate这些只是一些具体技术与工具,只要你掌握了一定的编程经验和编程理念,学这些框架不用花很多时间,在日常开发过程中,也会接触各种各样的新框架,新技术,技术这种东西是没有止境的,也许你今天会的东西,明天就已经过时了,所以不断的学习新的东西。
而一些理论基础在相当长的时间内是不会过时的,正如数学表达式解析,无论是使用java,还是使用c语言,本质上没有任何区别,考虑的一般都是堆栈或二叉树。
用java编写计算器用了什么技术和能够用Java编写一个计算器是什么水平了的问题分享结束啦,以上的文章解决了您的问题吗?欢迎您下次再来哦!