「java简单计算机程序」计算机语言Java

博主:adminadmin 2022-11-27 10:05:07 58

本篇文章给大家谈谈java简单计算机程序,以及计算机语言Java对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用Java编写一个简单的计算器程序

import java.awt.*;

import java.awt.event.*;

public class CalcAppDemo extends Frame{

private TextField t_result;

private Panel p_main; //主面板

private Panel p_num; //数字面板

private Panel p_oper; //操作符面板

private Panel p_show; //显示面板

private Button b_num[]; //数字按钮

private Button b_oper[]; //操作符面板

public CalcAppDemo(String title){

setTitle(title);

t_result = new TextField("0.0", 21);

p_main = new Panel();

p_num = new Panel();

p_oper = new Panel();

p_show = new Panel();

p_main.setLayout(new BorderLayout());

p_num.setLayout(new GridLayout(4, 3, 1, 1));

p_oper.setLayout(new GridLayout(4, 2, 1, 1));

b_num = new Button[12];

for(int i=0; i9; i++)

{

b_num[i] = new Button(new Integer(i+1).toString());

}

b_num[9] = new Button("0");

b_num[10] = new Button("cls");

b_num[11] = new Button(".");

for(int i=0; i12; i++)

{

p_num.add(b_num[i]);

}

b_oper = new Button[8];

b_oper[0] = new Button("+");

b_oper[1] = new Button("-");

b_oper[2] = new Button("*");

b_oper[3] = new Button("/");

b_oper[4] = new Button("pow");

b_oper[5] = new Button("sqrt");

b_oper[6] = new Button("+/-");

b_oper[7] = new Button("=");

for(int i=0; i8; i++) //

{

p_oper.add(b_oper[i]);

}

t_result.setEditable(false);

p_show.add(t_result, BorderLayout.NORTH);

p_main.add(p_show, BorderLayout.NORTH);

p_main.add(p_num, BorderLayout.WEST);

p_main.add(p_oper, BorderLayout.EAST);

this.add(p_main, BorderLayout.CENTER);

setSize(400, 400);

setResizable(false);

pack();

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

ButtonListener b1 = new ButtonListener();

for(int i=0; i12; i++)

{

b_num[i].addActionListener(b1);

}

for(int i=0; i8; i++)

{

b_oper[i].addActionListener(b1);

}

}

class ButtonListener implements ActionListener

{

private String lastOp; //存储上一此操作符

private String strVal; //存储数字对应的字符串

private double total; //总数

private double number; //存储新输入的数

private boolean firsttime; //判断是否第一次按下的是操作符按钮

private boolean operatorPressed;//判断是否已经按过操作符按钮

ButtonListener()

{

firsttime = true;

strVal = "";

}

//事件处理器

public void actionPerformed(ActionEvent e)

{

String s = ((Button)e.getSource()).getLabel().trim();

if(Character.isDigit(s.charAt(0)))

{//判断是操作数还是操作符

handleNumber(s);

}

else

{

calculate(s);

}

}

//判断是一元操作符还是二元操作符,并根据操作符类型做计算

void calculate(String op)

{

operatorPressed = true;

if(firsttime! isUnary(op))

{

total = getNumberOnDisplay();

firsttime = false;

}

if(isUnary(op))

{

handleUnaryOp(op);

}

else if(lastOp != null)

{

handleBinaryOp(lastOp);

}

if(! isUnary(op))

{

lastOp = op;

}

}

//判断是否一元操作符

boolean isUnary(String s)

{

return s.equals("=")

||s.equals("cls")||s.equals("sqrt")

||s.equals("+/-")||s.equals(".");

}

//处理一元操作符

void handleUnaryOp(String op)

{

if(op.equals("+/-"))

{//

number = negate(getNumberOnDisplay() + "");

t_result.setText("");

t_result.setText(number + "");

return;

}else if(op.equals("."))

{

handleDecPoint();

return;

}else if(op.equals("sqrt"))

{

number = Math.sqrt(getNumberOnDisplay());

t_result.setText("");

t_result.setText(number + "");

return;

}else if(op.equals("="))

{//

if(lastOp!= null !isUnary(lastOp))

{

handleBinaryOp(lastOp);

}

lastOp = null;

firsttime = true;

return;

}else

{

clear();

}

}

//处理二元运算符

void handleBinaryOp(String op)

{

if(op.equals("+"))

{

total +=number;

}else if(op.equals("-"))

{

total -=number;

}else if(op.equals("*"))

{

total *=number;

}else if(op.equals("/"))

{

try

{

total /=number;

}catch(ArithmeticException ae){}

}else if(op.equals("pow"))

total = Math.pow(total, number);

//t_result.setText("");

lastOp = null;

// strVal = "";

number = 0;

t_result.setText(total + "");

}

//该方法用于处理数字按钮

void handleNumber(String s)

{

if(!operatorPressed)

{

strVal += s;

}else

{

operatorPressed = false;

strVal = s;

}

//

number = new Double(strVal).doubleValue();

t_result.setText("");

t_result.setText(strVal);

}

//该方法用于按下"."按钮

void handleDecPoint()

{

operatorPressed = false;

//

if(strVal.indexOf(".")0)

{

strVal += ".";

}

t_result.setText("");

t_result.setText(strVal);

}

//该方法用于将一个数求反

double negate(String s)

{

operatorPressed = false;

//如果是一个整数,去掉小数点后面的0

if(number == (int)number)

{

s = s.substring(0,s.indexOf("."));

}

//如果无"-"增加在该数的前面

if(s.indexOf("-")0)

{

strVal = "-" + s;

}

else

{

strVal = s.substring(1);

}

return new Double(strVal).doubleValue();

}

//将显示框中的值转换成Double

double getNumberOnDisplay()

{

return new Double(t_result.getText()).doubleValue();

}

//清除屏幕并设置所有的标识

void clear()

{

firsttime = true;

lastOp = null;

strVal = "";

total = 0;

number = 0;

t_result.setText("0");

}

}

public static void main(String[] args) {

CalcAppDemo c = new CalcAppDemo("简单的计算器程序");

c.setVisible(true);

}

}

用JAVA编写一个简单的计算器,要求如下:

然后 通过输入 显示结果,比如说:

以下是上图计算器的代码:

package Computer;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Stack;

import javax.swing.JApplet;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Count extends JApplet implements ActionListener

{

/**

*

*/

private static final long serialVersionUID = 1L;

private JTextField textField = new JTextField("请输入");

String operator = "";//操作

String input = "";//输入的 式子

boolean flag =  true;

//  boolean flag1 = true;

//  boolean flag2 = true;

public void init()//覆写Applet里边的init方法

{

Container C = getContentPane();

JButton b[] = new JButton[16];

JPanel panel = new JPanel();

C.add(textField, BorderLayout.NORTH);

C.add(panel,BorderLayout.CENTER);

panel.setLayout(new GridLayout(4, 4,5,5));

String name[]={"7","8","9","+","4","5","6","-","1","2","3","*","0","C","=","/"};//设置 按钮

for(int i=0;i16;i++)//添加按钮

{

b[i] = new JButton(name[i]);

b[i].setBackground(new Color(192,192,192));

b[i].setForeground(Color.BLUE);//数字键 设置为 蓝颜色

if(i%4==3)

b[i].setForeground(Color.RED);

b[i].setFont(new Font("宋体",Font.PLAIN,16));//设置字体格式

panel.add(b[i]);

b[i].addActionListener(this);

}

b[13].setForeground(Color.RED);//非数字键,即运算键设置为红颜色

b[13].setForeground(Color.RED);

}

public void actionPerformed(ActionEvent e)

{

int cnt = 0;

String actionCommand = e.getActionCommand();

if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("*") ||actionCommand.equals("/"))

input +=" "+actionCommand+" ";//设置输入,把输入的样式改成 需要的样子

else if(actionCommand.equals("C"))

input = "";

else if(actionCommand.equals("="))//当监听到等号时,则处理 input

{

input+= "="+compute(input);

textField.setText(input);

input="";

cnt = 1;

}

else

input += actionCommand;//数字为了避免多位数的输入 不需要加空格

if(cnt==0)

textField.setText(input);

}

private String compute(String input)//即1237 的 样例

{

String str[];

str = input.split(" ");

StackDouble s = new StackDouble();

double m = Double.parseDouble(str[0]);

s.push(m);

for(int i=1;istr.length;i++)

{

if(i%2==1)

{

if(str[i].compareTo("+")==0)

{

double help = Double.parseDouble(str[i+1]);

s.push(help);

}

if(str[i].compareTo("-")==0)

{

double help = Double.parseDouble(str[i+1]);

s.push(-help);

}

if(str[i].compareTo("*")==0)

{

double help = Double.parseDouble(str[i+1]);

double ans = s.peek();//取出栈顶元素

s.pop();//消栈

ans*=help;

s.push(ans);

}

if(str[i].compareTo("/")==0)

{

double help = Double.parseDouble(str[i+1]);

double ans = s.peek();

s.pop();

ans/=help;

s.push(ans);

}

}

}

double ans = 0d;

while(!s.isEmpty())

{

ans+=s.peek();

s.pop();

}

String result = String.valueOf(ans);

return result;

}

public static void main(String args[])

{

JFrame frame = new JFrame("Count");

Count applet = new Count();

frame.getContentPane().add(applet, BorderLayout.CENTER);

applet.init();//applet的init方法

applet.start();//线程开始

frame.setSize(350, 400);//设置窗口大小

frame.setVisible(true);//设置窗口可见

}

}

求一个用JAVA编写的计算器程序(1)实现简单加、减、乘、除的运算。 (

public

MyCalculator()

{

f

=

new

JFrame("计算器ByMdou");

Container

contentPane

=

f.getContentPane();

/****************菜单的创建开始**************************************/

JMenuBar

mBar

=

new

JMenuBar();

mBar.setOpaque(true);

mEdit

=

new

JMenu("编辑(E)");

mEdit.setMnemonic(KeyEvent.VK_E);

mCopy

=

new

JMenuItem("复制(C)");

mEdit.add(mCopy);

mPaste

=

new

JMenuItem("粘贴(P)");

mEdit.add(mPaste);

mView

=

new

JMenu("查看(V)");

mView.setMnemonic(KeyEvent.VK_V);

mView.add(new

JMenuItem("标准型"));

mView.add(new

JMenuItem("科学型"));

mView.addSeparator();

mView.add(new

JMenuItem("查看分组"));

mHelp

=

new

JMenu("帮助(H)");

mHelp.setMnemonic(KeyEvent.VK_H);

mHelp.add(new

JMenuItem("帮助主题"));

mHelp.addSeparator();

mHelp.add(new

JMenuItem("关于计算器"));

mBar.add(mEdit);

mBar.add(mView);

mBar.add(mHelp);

f.setJMenuBar(mBar);

contentPane.setLayout(new

BorderLayout

());

JPanel

pTop

=

new

JPanel();

tResult

=

new

JTextField("0.",26);

tResult.setHorizontalAlignment(JTextField.RIGHT

);

tResult.setEditable(false);

pTop.add(tResult);

contentPane.add(pTop,BorderLayout.NORTH);

JPanel

pBottom

=

new

JPanel();

pBottom.setLayout(new

BorderLayout());

JPanel

pLeft

=

new

JPanel();

pLeft.setLayout(new

GridLayout(5,1,3,3));

bM

=

new

JButton("

");

bM.setEnabled(false);

pLeft.add(bM);

bOther

=

new

JButton("MC");

bOther.addActionListener(this);

bOther.setForeground(Color.RED);

bOther.setMargin(new

Insets(3,2,3,2));

pLeft.add(bOther);

bOther

=

new

JButton("MR");

bOther.addActionListener(this);

bOther.setForeground(Color.RED);

bOther.setMargin(new

Insets(3,2,3,2));

pLeft.add(bOther);

bOther

=

new

JButton("MS");

bOther.addActionListener(this);

bOther.setForeground(Color.RED);

bOther.setMargin(new

Insets(3,2,3,2));

pLeft.add(bOther);

bOther

=

new

JButton("M+");

bOther.addActionListener(this);

bOther.setForeground(Color.RED);

bOther.setMargin(new

Insets(3,2,3,2));

pLeft.add(bOther);

用Java写一个计算机的程序怎么写啊拜托了各位 谢谢

import java.awt.BorderLayout; import java.awt.Button; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.WindowConstants; public class Caculator extends JFrame implements ActionListener,KeyListener{ private JTextField tf=new JTextField(); private float x=0; private float y=0; private int code=0; private boolean enable; private boolean first; private String str=""; public Caculator(){ Container ct=this.getContentPane(); this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); tf.setHorizontalAlignment(JTextField.RIGHT); //tf.setText("0"); enable=true; first=true; ct.add(tf,BorderLayout.NORTH); JPanel panel=new JPanel(); panel.setLayout(new GridLayout(4,4)); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(Caculator.this,"确定要关闭程序吗?","提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE)){ e.getWindow().setVisible(false); e.getWindow().dispose(); System.exit(0); } } }); Button btn=null; btn=new Button("1"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("2"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("3"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("+"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("4"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("5"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("6"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("-"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("7"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("8"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("9"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("*"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("0"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("."); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("/"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("="); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); this.add(panel,BorderLayout.CENTER); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Caculator mainframe=new Caculator(); mainframe.setTitle("testing Caculator"); mainframe.setSize(400,400); mainframe.setVisible(true); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand()=="+"){ x= Float.parseFloat(tf.getText()); code=0; this.tf.setText(""); } if(e.getActionCommand()=="-"){ x= Float.parseFloat(tf.getText()); code=1; this.tf.setText(""); } if(e.getActionCommand()=="*"){ x= Float.parseFloat(tf.getText()); code=2; this.tf.setText(""); } if(e.getActionCommand()=="/"){ x= Float.parseFloat(tf.getText()); code=3; this.tf.setText(""); } if(e.getActionCommand()!="+"e.getActionCommand()!="-"e.getActionCommand()!="*"e.getActionCommand()!="/"e.getActionCommand()!="="){ if(enable){ if(first){ System.out.println("haha"); tf.setText(e.getActionCommand()); first=false; } else { tf.setText(tf.getText()+e.getActionCommand()); } } else { tf.setText(e.getActionCommand()); enable=true; } } if(e.getActionCommand()=="="){ switch(code){ case 0: y=x+Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 1: y=x-Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 2: y=x*Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 3: y=x/Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; } } } public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyChar()=='+'){ x= Float.parseFloat(tf.getText()); code=0; this.tf.setText(""); } if(e.getKeyChar()=='-'){ x= Float.parseFloat(tf.getText()); code=1; this.tf.setText(""); } if(e.getKeyChar()=='*'){ x= Float.parseFloat(tf.getText()); code=2; this.tf.setText(""); } if(e.getKeyChar()=='/'){ x= Float.parseFloat(tf.getText()); code=3; this.tf.setText(""); } if(e.getKeyChar()=='1'||e.getKeyChar()=='2'||e.getKeyChar()=='3'||e.getKeyChar()=='0' ||e.getKeyChar()=='4'||e.getKeyChar()=='5'||e.getKeyChar()=='6'||e.getKeyChar()=='.' ||e.getKeyChar()=='7'||e.getKeyChar()=='8'||e.getKeyChar()=='9'){ System.out.println("hai"); if(enable){ if(first){ System.out.println("hehe"); str=Character.toString(e.getKeyChar()); tf.setText(str); first=false; } else { str=Character.toString(e.getKeyChar()); tf.setText(tf.getText()+str); } } else { str=Character.toString(e.getKeyChar()); tf.setText(str); enable=true; } } if(e.getKeyCode()==KeyEvent.VK_ENTER){ switch(code){ case 0: y=x+Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 1: y=x-Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 2: y=x*Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 3: y=x/Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; } } } public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }

java简单计算机程序的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于计算机语言Java、java简单计算机程序的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。