导航:首页 > 编程语言 > java计算器

java计算器

发布时间:2022-01-17 15:02:10

java简易计算器

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener
{

String s="",s1;
double d1,d2;
JFrame jf = new JFrame("小计算器by Graate") ;

JTextField tf = new JTextField();

public void init()//实现计算器界面
{
Container c=jf.getContentPane();
tf.setHorizontalAlignment(JTextField.RIGHT);//文本框
c.add(tf,"North");

JPanel pn3 = new JPanel(new BorderLayout());
c.add(pn3,"Center");

JPanel pn2 = new JPanel();//功能键界面(清除键和关闭键)
pn2.setLayout(new BorderLayout());

JPanel pn1 = new JPanel();//运算界面

pn1.setLayout(new GridLayout(4,4));

pn3.add(pn2,"North");
pn3.add(pn1);

//设置按钮
JButton b = new JButton("CLEAR");
b.setToolTipText("请按清除键!");//设置清零键
b.setForeground(Color.RED);//设置字体颜色
b.setBackground(Color.YELLOW);//设置背景色
b.addActionListener(this);
pn2.add(b,"Center");
b = new JButton("OFF");
b.setToolTipText("请按退出键!");//设置off键,点击退出应用程序b.addActionListener(this);
b.setForeground(Color.RED);//字体颜色
b.setBackground(Color.ORANGE);//背景色
pn2.add(b,"East");
b = new JButton("1");//add butten 1
b.addActionListener(this);
pn1.add(b);
b = new JButton("2");//add butten 2
b.addActionListener(this);
pn1.add(b);
b = new JButton("3");//add butten 3
b.addActionListener(this);
pn1.add(b);
b = new JButton("+");//add butten +
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("4");//add butten 4
b.addActionListener(this);
pn1.add(b);
b = new JButton("5");//add butten 5
b.addActionListener(this);
pn1.add(b);
b = new JButton("6");//add button 6
b.addActionListener(this);
pn1.add(b);
b = new JButton("-");//add button -
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("7");//add button 7
b.addActionListener(this);
pn1.add(b);
b = new JButton("8");//add button 8
b.addActionListener(this);
pn1.add(b);
b = new JButton("9");//add button 9
b.addActionListener(this);
pn1.add(b);
b = new JButton("*");//add button *
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("0");//add button 0
b.addActionListener(this);
pn1.add(b);
b = new JButton(".");//add button .
b.addActionListener(this);
pn1.add(b);
b = new JButton("=");//add button =
b.setForeground(Color.RED);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("\\");//add button \
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);

jf.setSize(300,300);//设置大小
jf.setVisible(true);//设置为可视
}

//处理按钮按下时的动作,进行相应的处理
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
tf.setText(tf.getText()+command);
if(command.equals("CLEAR")) //清零键 按下时返回初始状态
{
s1=null;
s="";
tf.setText("");//记录输入值的变量清空
}

else if(command.equals("OFF")) System.exit(0);//off键 关闭应用程序

else if(!command.equals("*")&&!command.equals("\\")
&&!command.equals("+")&&!command.equals("-")
&&!command.equals("="))//判断输入是否为数字
{
if(s1==null)//判断输入是否为第一个
s1 = command;
else s1+=command;
d1 = new Double(s1).doubleValue();//字符串型转换为双精度型,还原输入数字
try
{
if(s.equals("+")) d1 = d1+d2;//加法运算
else if(s.equals("-")) d1 = d2-d1;//减法运算
else if(s.equals("*")) d1 = d1*d2;//乘法运算
else if(s.equals("\\"))d1 = d2/d1;//除法运算
}
catch(Exception ex)
{
tf.setText("Error");//错误显示"Error"
System.out.println(ex.getMessage());
}

}

else if(!command.equals("=")) //判断输入是否为+ - * \
{
s = command;
s1 = null;
d2 = d1;
}

else//输入=时,显示运算结果
{

tf.setText(tf.getText()+d1);

}

}

public static void main(String [] args)
{

new Calculator().init();

}
}

⑵ java编写计算器!

老表啊,你不是要学Java吗,这种基础的东西你应该自己来啊,这是Java的第一步,要走的踏实些。

⑶ 用JAVA编写一个计算器

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JCalculator extends JFrame implements ActionListener {
private static final long serialVersionUID = -L;
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}

int i;
private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*", "1",
"2", "3", "-", ".", "0", "=", "+" };
// 建立按钮
JButton[] buttons = new JButton[str.length];
// 撤销重置
JButton reset = new JButton("CE");
// 建立文本域显示结果
JTextField display = new JTextField("0");

public JCalculator() {
super("Calculator");
// 添加一个面板
JPanel panel1 = new JPanel(new GridLayout(4, 4));
// panel1.setLayout(new GridLayout(4,4));
for (i = 0; i < str.length; i++) {
buttons[i] = new JButton(str[i]);
panel1.add(buttons[i]);
}
JPanel panel2 = new JPanel(new BorderLayout());
// panel2.setLayout(new BorderLayout());
panel2.add("Center", display);
panel2.add("East", reset);
// JPanel panel3 = new Panel();
getContentPane().setLayout(new BorderLayout());
getContentPane().add("North", panel2);
getContentPane().add("Center", panel1);
// 添加操作动作的监听器.
for (i = 0; i < str.length; i++)
buttons[i].addActionListener(this);
// 为重置按钮添加监听器
reset.addActionListener(this);
display.addActionListener(this);
// The "close" button "X".
addWindowListener(new WindowCloser());
// Initialize the window size.
setSize(800, 800);
// Show the window.
// show(); Using show() while JDK version is below 1.5.
setVisible(true);
// Fit the certain size.
pack();
}

public void actionPerformed(ActionEvent e) {
Object target = e.getSource();
String label = e.getActionCommand();
if (target == reset)
handleReset();
else if (".".indexOf(label) > 0)
handleNumber(label);
else
handleOperator(label);
}
// Is the first digit pressed?
boolean isFirstDigit = true;
/**
* Number handling.
* @param key the key of the button.
*/
public void handleNumber(String key) {
if (isFirstDigit)
display.setText(key);
else if ((key.equals(".")) (display.getText().indexOf(".") < 0))
display.setText(display.getText() + ".");
else if (!key.equals("."))
display.setText(display.getText() + key);
isFirstDigit = false;
}

/**
* Reset the calculator.
*/
public void handleReset() {
display.setText("0");
isFirstDigit = true;
operator = "=";
}

double number = 0.0;
String operator = "=";

public void handleOperator(String key) {
if (operator.equals("+"))
number += Double.valueOf(display.getText());
else if (operator.equals("-"))
number -= Double.valueOf(display.getText());
else if (operator.equals("*"))
number *= Double.valueOf(display.getText());
else if (operator.equals("/"))
number /= Double.valueOf(display.getText());
else if (operator.equals("="))
number = Double.valueOf(display.getText());
display.setText(String.valueOf(number));
operator = key;
isFirstDigit = true;
}

public static void main(String[] args) {
new JCalculator();
}
}

⑷ java简单计算器

import java.util.Scanner;

public class Calculator {
@SuppressWarnings("resource")
public static void main(String[] args) {
System.out.println("+------------------+");
System.out.println("| JAVA计算器例子 |");
System.out.println("+------------------+");
System.out.println("规则:操作数必须为整数,若除法运算则除数不能为零,");
System.out.println("运算符必须为'+,-,*,%'中的一种");
while (true) {
System.out.println("+-------------------------------------------+");
System.out.println("| + 选 1,- 选 2,* 选 3,/ 选 4,退出 选 0 |");
System.out.println("+-------------------------------------------+");
System.out.println("请输入您的选择:");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if ("0".equals(input)) {
System.exit(0);
break;
} else {
calculation(input);
}
}
}
@SuppressWarnings("resource")
private static void calculation(String param) {
System.out.println("请输入第一个数字:");
Scanner scanner1 = new Scanner(System.in);
int num1 = scanner1.nextInt();
System.out.println("请输入第二个数字(如果是除法,不能输入0):");
Scanner scanner2 = new Scanner(System.in);
int num2 = scanner2.nextInt();
switch (param) {
case "1" :
System.out.println("本次的计算结果为:" + (num1 + num2));
break;
case "2" :
System.out.println("本次的计算结果为:" + (num1 - num2));
break;
case "3" :
System.out.println("本次的计算结果为:" + (num1 * num2));
break;
case "4" :
if (num2 == 0) {
System.out.println("除数为零,计算失败!!!");
} else {
System.out.println("本次的计算结果为:" + (num1 / num2));
}
break;
default :
System.out.println("输入有误,请重新输入。");
break;
}
}
}
上面是我写的代码,麻烦您看一下是否能满足要求。
下面是执行的结果:
+------------------+
| JAVA计算器例子 |
+------------------+
规则:操作数必须为整数,若除法运算则除数不能为零,
运算符必须为'+,-,*,%'中的一种
+-------------------------------------------+
| + 选 1,- 选 2,* 选 3,/ 选 4,退出 选 0 |
+-------------------------------------------+
请输入您的选择:
1
请输入第一个数字:
3
请输入第二个数字(如果是除法,不能输入0):
4
本次的计算结果为:7
+-------------------------------------------+
| + 选 1,- 选 2,* 选 3,/ 选 4,退出 选 0 |
+-------------------------------------------+
请输入您的选择:
5
请输入第一个数字:
0
请输入第二个数字(如果是除法,不能输入0):
6
输入有误,请重新输入。
+-------------------------------------------+
| + 选 1,- 选 2,* 选 3,/ 选 4,退出 选 0 |
+-------------------------------------------+
请输入您的选择:
4
请输入第一个数字:
6
请输入第二个数字(如果是除法,不能输入0):
3
本次的计算结果为:2
+-------------------------------------------+
| + 选 1,- 选 2,* 选 3,/ 选 4,退出 选 0 |
+-------------------------------------------+
请输入您的选择:
2
请输入第一个数字:
7
请输入第二个数字(如果是除法,不能输入0):
2
本次的计算结果为:5
+-------------------------------------------+
| + 选 1,- 选 2,* 选 3,/ 选 4,退出 选 0 |
+-------------------------------------------+
请输入您的选择:
3
请输入第一个数字:
7
请输入第二个数字(如果是除法,不能输入0):
8
本次的计算结果为:56
+-------------------------------------------+
| + 选 1,- 选 2,* 选 3,/ 选 4,退出 选 0 |
+-------------------------------------------+
请输入您的选择:
0

⑸ java 编计算器

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CalculatorGUI {
private Frame f;
private Panel p1, p2;
private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private Button bPoint, bAdd, bDec, bMul, bDiv, bCal;
private TextField tf;
private String s, op;
private Calculator cal = new Calculator();
private boolean ifOp;

public CalculatorGUI() {
f = new Frame("Calculator");
p1 = new Panel();
p2 = new Panel();

b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bPoint = new Button(".");
bAdd = new Button("+");
bDec = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bCal = new Button("=");

tf = new TextField(25);
tf.setEditable(false);

}

public void launchFrame() {
f.setSize(220, 160);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1, BorderLayout.NORTH);
p2.setLayout(new GridLayout(4, 4));

b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.addActionListener(new setLabelText_ActionListener());
bPoint.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bDec.addActionListener(new setOperator_ActionListener());
bMul.addActionListener(new setOperator_ActionListener());
bDiv.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());

p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bAdd);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bDec);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bPoint);
p2.add(bCal);
p2.add(bDiv);
f.add(p2, BorderLayout.SOUTH);
f.setVisible(true);
}

public void setTextFieldText_Temp() {
if (tf.getText().length() < 15
&& (tf.getText().indexOf(".") == -1 || !s.equals("."))) {
tf.setText(tf.getText() + s);
} else {
tf.setText((tf.getText() + s).substring(0, 15));
}
}

public void setTextFieldText() {
if (ifOp) {
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
} else {
setTextFieldText_Temp();
}
}

public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
calculator.launchFrame();
}

class myWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}

class setLabelText_ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button tempB = (Button) e.getSource();
s = tempB.getLabel();
setTextFieldText();
}
}

class setOperator_ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button tempB = (Button) e.getSource();
op = tempB.getLabel();
if (op.equals("+")) {
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
} else if (op.equals("-")) {
tf.setText(cal.opSubtract(tf.getText()));
ifOp = true;
} else if (op.equals("*")) {
tf.setText(cal.opMultiply(tf.getText()));
ifOp = true;
} else if (op.equals("/")) {
tf.setText(cal.opDivide(tf.getText()));
ifOp = true;
} else if (op.equals("=")) {
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
}
}
}

}

class Calculator {
private String result = "0";
private int op = 0, add = 1, sub = 2, mul = 3, div = 4;

private double stringToDouble(String x) {
double y = Double.parseDouble(x);
return y;
}

private void operate(String x) {
double x1 = stringToDouble(x);
double y = stringToDouble(result);
switch (op) {
case 0:
result = x;
break;
case 1:
result = String.valueOf(y + x1);
break;
case 2:
result = String.valueOf(y - x1);
break;
case 3:
result = String.valueOf(y * x1);
break;
case 4:
if (x1 != 0) {
result = String.valueOf(y / x1);
} else {
result = "The divisor can't be zero!";
}
break;
}
}

public String opAdd(String x) {
operate(x);
op = add;
return result;
}

public String opSubtract(String x) {
operate(x);
op = sub;
return result;
}

public String opMultiply(String x) {
operate(x);
op = mul;
return result;
}

public String opDivide(String x) {
operate(x);
op = div;
return result;
}

public String opEquals(String x) {
operate(x);
op = 0;
return result;
}

public void opClean() {
op = 0;
result = "0";
}
}

⑹ JAVA计算器代码

import java.awt.*;
import java.applet.*;

public class CalculatorDemo extends Applet
{
TextField answerText;
Button pointButton,equalButton,plusButton;
Button minusButton,clearButton,multiButton,divisionButton;
Button[] b=new Button[10];

String currentOp,preOp;
String foreText,backText;
boolean isFloat=false;
public void init()
{
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
currentOp=new String("");
preOp=new String("");
foreText=new String("");
backText=new String("");
answerText=new TextField(8);
setBackground(Color.lightGray);
setForeground(Color.blue);
for(int i=9;i>=0;i--)
{
b[i]=new Button(Integer.toString(i));
p2.add(b[i]);
}
pointButton=new Button(".");
equalButton=new Button("=");
equalButton.setForeground(Color.red);
clearButton=new Button("清除");
clearButton.setForeground(Color.red);
divisionButton=new Button("/");
divisionButton.setForeground(Color.red);
multiButton=new Button("*");
multiButton.setForeground(Color.red);
minusButton=new Button("-");
minusButton.setForeground(Color.red);
plusButton=new Button("+");
plusButton.setForeground(Color.red);
setLayout(new FlowLayout());
p1.setLayout(new FlowLayout());
p2.setLayout(new GridLayout(4,3));
p3.setLayout(new GridLayout(4,1));
p1.add(answerText);
p1.add(clearButton);
p2.add(pointButton);
p2.add(equalButton);
p3.add(plusButton);
p3.add(minusButton);
p3.add(multiButton);
p3.add(divisionButton);
add(p1);
add(p2);
add(p3);
}
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==pointButton)
{
if(e.target !=pointButton)
{
s=(String)o;
doForeText(s);
}
if((e.target==pointButton)&&(!isFloat))
{
isFloat=true;
s=(String)o;
if(foreText.equals(""))
{
foreText +="0.";
}
else
{
doForeText(s);
}
}
}
}
if(e.target==clearButton)
{
doClear();
}
if((e.target==clearButton)||(e.target==divisionButton)
||(e.target==plusButton)||(e.target==minusButton))
{
if(foreText !="")
{
currentOp=((String)o);
doOperator();
}
else
{
preOp=((String)o);
}
}
if(e.target==equalButton)
{
doOperator();
}
return true;
}
public void doOperator()
{
double dFore,dBack;
Double d;
if(preOp.equals(""))
{
backText=foreText;
foreText="";
answerText.setText(backText);
}
else
{
dFore=(new Double(foreText)).doubleValue();
dBack=(new Double(backText)).doubleValue();
foreText="";
backText=answerText.getText();

if(preOp.equals("+"))
{
d=new Double((dBack+dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("-"))
{
d=new Double((dBack-dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("*"))
{
d=new Double((dBack*dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("/"))
{
if(dFore==0)
{
answerText.setText("除数不能为0");
return;
}
d=new Double((dBack/dFore));
answerText.setText(d.toString());
backText=d.toString();
}
}
preOp=currentOp;
}
public void doForeText(String s)
{
foreText +=s;
answerText.setText(foreText);
}
public void doBackText(String s)
{
backText=foreText;
foreText="";
answerText.setText(foreText);
}
public void doClear()
{
currentOp="";
preOp="";
foreText="";
backText="";
isFloat=false;
answerText.setText("");
}
}

⑺ java 计算器

我自己写的:

import java.awt.*;//计算器实例
import java.awt.event.*;
public class calculator
{
public static void main(String args[])
{
MyWindow my=new MyWindow("计算器");
}
}

class MyWindow extends Frame implements ActionListener
{ StringBuffer m=new StringBuffer();
int p;
TextField tex;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,jia,jian,cheng,chu,deng,dian,qingling,kaifang;

MyWindow(String s)
{
super(s);
//StringBuffer s2=new StringBuffer();
//String s;

tex=new TextField(18);
b0=new Button(" 0 ");
b1=new Button(" 1 ");
b2=new Button(" 2 ");
b3=new Button(" 3 ");
b4=new Button(" 4 ");
b5=new Button(" 5 ");
b6=new Button(" 6 ");
b7=new Button(" 7 ");
b8=new Button(" 8 ");
b9=new Button(" 9 ");
dian=new Button(" . ");
jia=new Button(" + ");
jian=new Button(" - ");
cheng=new Button(" × ");
chu=new Button(" / ");
deng=new Button(" = ");
qingling=new Button(" 清零 ");
kaifang=new Button(" √ ");

setLayout(new FlowLayout());
add(tex);
add(b0);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(dian);
add(jia);

add(jian);

add(cheng);

add(chu);
add(kaifang);
add(qingling);
add(deng);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
dian.addActionListener(this);
deng.addActionListener(this);
qingling.addActionListener(this);
kaifang.addActionListener(this);

setBounds(200,200,160,280);
setResizable(false);//不可改变大小
setVisible(true);
validate();

addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent ee)
{ System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0)
{
m=m.append("0");

tex.setText(String.valueOf(m));
}

if(e.getSource()==b1)
{
m=m.append("1"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b2)
{
m=m.append("2"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b3)
{
m=m.append("3"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b4)
{
m=m.append("4"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b5)
{
m=m.append("5"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b6)
{
m=m.append("6"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b7)
{
m=m.append("7"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b8)
{
m=m.append("8"); tex.setText(String.valueOf(m));
}

if(e.getSource()==b9)
{
m=m.append("9"); tex.setText(String.valueOf(m));
}
if(e.getSource()==jia)
{
m=m.append("+"); tex.setText(String.valueOf(m));
}

if(e.getSource()==jian)
{
m=m.append("-"); tex.setText(String.valueOf(m));
}
if(e.getSource()==cheng)
{
m=m.append("*"); tex.setText(String.valueOf(m));
}
if(e.getSource()==chu)
{
m=m.append("/"); tex.setText(String.valueOf(m));
}

if(e.getSource()==dian)
{
m=m.append("."); tex.setText(String.valueOf(m));
}
String mm=String.valueOf(m);
int p1=mm.indexOf("+");
int p2=mm.indexOf("-");
int p3=mm.indexOf("*");
int p4=mm.indexOf("/");
if(p1!=-1)
{
p=p1;
}

else if(p3!=-1)
{
p=p3;
}
else if(p2!=-1)
{
p=p2;
}
else if(p4!=-1)
{
p=p4;
}

if(e.getSource()==deng)
{

String m1=mm.substring(0,p);
String m2=mm.substring(p+1);
String ch=mm.substring(p,p+1);

//System.out.println(m1);
//System.out.println(m2);
//System.out.println(ch);

if(ch.equals("+"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1+n2;
String su=String.valueOf(sum);
tex.setText(su);
}

if(ch.equals("-"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1-n2;
String su=String.valueOf(sum);
tex.setText(su);
}

if(ch.equals("*"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1*n2;
String su=String.valueOf(sum);
tex.setText(su);
}

if(ch.equals("/"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1/n2;
String su=String.valueOf(sum);
tex.setText(su);
}

}

if(e.getSource()==qingling)
{StringBuffer kk=new StringBuffer();
m=kk;

tex.setText("0");

// System.out.println(mm);
}

if(e.getSource()==kaifang)
{
String t=tex.getText();
float num=Float.parseFloat(t);
double nub=Math.sqrt(num);
tex.setText(String.valueOf(nub));

}

}

}

⑻ java计算器

<!DOCTYPEHTML>
<html>
<head>
<title>简易计算器</title>
<metacharset="utf-8">
<script>
functionvalidate(str,id){
if(isNaN(str)){
alert("请输入数字");
document.getElementById(id).value='';
}

}
functionfnOperate(op){
varnum1=document.getElementById("txtNum1").value,
num2=document.getElementById("txtNum2").value,
result=document.getElementById("txtResult");
console.log(num1);
console.log(num2);
if(op=="+"){
result.value=eval(num1+op+num2);
}elseif(op=="-"){
result.value=eval(num1+op+num2);
}elseif(op=="*"){
result.value=eval(num1+op+num2);
}elseif(op=="/"){
if(num2!=0){
result.value=eval(num1+op+num2);
}else{
alert("除数不能为0");
return;
}
}

}
</script>
</head>
<body>
<form>
<table>
<tr>
<tdcolspan="4"><h3>简易计算器</h3></td>
</tr>
<tr>
<td>第一个数</td>
<tdcolspan="3">
<inputid="txtNum1"type="text"size="25"onBlur="validate(this.value,'txtNum1');">
</td>
</tr>
<tr>
<td>第二个数</td>
<tdcolspan="3">
<inputid="txtNum2"type="text"size="25"onBlur="validate(this.value,'txtNum2');">
</td>
</tr>
<tr>
<td>
<inputid="addButton"type="button"value="+"onClick="fnOperate('+')">
</td>
<td>
<inputid="subButton"type="button"value="-"onClick="fnOperate('-')">
</td>
<td>
<inputid="mulButton"type="button"value="×"onClick="fnOperate('*')">
</td>
<td>
<inputid="divButton"type="button"value="÷"onClick="fnOperate('/')">
</td>
</tr>
<tr>
<td>计算结果</td>
<tdcolspan="3"><inputid="txtResult"type="text"size="25"></td>
</tr>
</table>
</form>
</body>
</html>

⑼ java计算器代码

最好还是自己多点打代码,又不是很难,这只是入门级别的小工具而已,你去csdn一搜一大推

⑽ 怎么做一个JAVA计算器

classCalcBtnextendsJButton{

=1L;

Fontfont=newFont("仿宋",1,22);

publicCalcBtn(Stringtext,Colorcolor){

super(text);

setForeground(color);

setFont(font);

setMargin(newInsets(0,0,0,0));

setFocusable(false);

}

}

@SuppressWarnings("serial")

{

publicstaticvoidmain(String[]args){

try{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch(Exceptionex){

ex.printStackTrace();

}

newCalcFrame().setVisible(true);

}

privateCalcBtnbtn_1=newCalcBtn("1",Color.BLUE);

privateCalcBtnbtn_2=newCalcBtn("2",Color.BLUE);

privateCalcBtnbtn_3=newCalcBtn("3",Color.BLUE);

privateCalcBtnbtn_4=newCalcBtn("4",Color.BLUE);

privateCalcBtnbtn_5=newCalcBtn("5",Color.BLUE);

privateCalcBtnbtn_6=newCalcBtn("6",Color.BLUE);

privateCalcBtnbtn_7=newCalcBtn("7",Color.BLUE);

privateCalcBtnbtn_8=newCalcBtn("8",Color.BLUE);

privateCalcBtnbtn_9=newCalcBtn("9",Color.BLUE);

privateCalcBtnbtn_0=newCalcBtn("0",Color.BLUE);

privateCalcBtnbtn_back=newCalcBtn("←",Color.red);

privateCalcBtnbtn_c=newCalcBtn("C",Color.red);

privateCalcBtnbtn_ce=newCalcBtn("CE",Color.red);

privateCalcBtnbtn_sub=newCalcBtn("-",Color.red);

privateCalcBtnbtn_multi=newCalcBtn("*",Color.red);

privateCalcBtnbtn_div=newCalcBtn("/",Color.red);

privateCalcBtnbtn_add=newCalcBtn("+",Color.red);

privateCalcBtnbtn_sqrt=newCalcBtn("sqrt",Color.BLUE);

privateCalcBtnbtn_mod=newCalcBtn("%",Color.BLUE);

privateCalcBtnbtn_x=newCalcBtn("1/x",Color.BLUE);

privateCalcBtnbtn_res=newCalcBtn("=",Color.red);

privateCalcBtnbtn_point=newCalcBtn(".",Color.red);

privateCalcBtnbtn_flag=newCalcBtn("+/-",Color.BLUE);

privateJButtonbtn_mem=newJButton("");

privateCalcBtnbtn_m=newCalcBtn("M+",Color.BLUE);

privateCalcBtnbtn_mc=newCalcBtn("MC",Color.BLUE);

privateCalcBtnbtn_mr=newCalcBtn("MR",Color.BLUE);

privateCalcBtnbtn_ms=newCalcBtn("MS",Color.BLUE);

JPanelpan_jtf=newJPanel();

JPanelpan_back=newJPanel();

JPanelpan_mem=newJPanel();

JPanelpan_num=newJPanel();

JTextFieldjtf_res=newJTextField();

//

privateJMenuBarmenuBar=newJMenuBar();

privateJMenumenu_edit=newJMenu("编辑(E)");

privateJMenumenu_view=newJMenu("查看(V)");

privateJMenumenu_help=newJMenu("帮助(H)");

privateJMenuItemeditMenu_=newJMenuItem("复制(C)");

privateJMenuItemeditMenu_paste=newJMenuItem("粘贴(P)");

_standard=newJCheckBoxMenuItem(

"标准型(T)");

_science=newJCheckBoxMenuItem("科学型(S)");

_grouping=newJCheckBoxMenuItem(

"数字分组(I)");

privateJMenuItemhelpmenu_help=newJMenuItem("帮助主题(H)");

privateJMenuItemhelpmenu_about=newJMenuItem("关于计算器(A)");

Stringsave1,save2;

BigDecimalop1,op2;

booleanisBigDecimal=false;

intpreOp,currentOp=0;

StringBufferstr=newStringBuffer();

publicCalcFrame(){

this.setTitle("于☆弦");

this.setResizable(false);

setSize(444,390);

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setJMenuBar(menuBar);

setLayout(null);

init_pan_jtf();

init_menuBar();

init_pan_back();

init_pan_mem();

init_pan_num();

getContentPane().add(pan_jtf);

getContentPane().add(pan_back);

getContentPane().add(pan_mem);

getContentPane().add(pan_num);

}

privatevoidinit_menuBar(){

menuBar.add(menu_edit);

menuBar.add(menu_view);

menuBar.add(menu_help);

menu_edit.add(editMenu_);

menu_edit.add(editMenu_paste);

menu_view.add(viewmenu_standard);

menu_view.add(viewmenu_science);

menu_view.addSeparator();

menu_view.add(viewmenu_grouping);

menu_help.add(helpmenu_help);

menu_help.addSeparator();

menu_help.add(helpmenu_about);

menu_edit.setMnemonic('E');

menu_view.setMnemonic('V');

menu_help.setMnemonic('H');

editMenu_.setMnemonic('C');

editMenu_.setAccelerator(KeyStroke.getKeyStroke('C',

InputEvent.CTRL_MASK));

editMenu_paste.setMnemonic('P');

editMenu_paste.setAccelerator(KeyStroke.getKeyStroke('V',

InputEvent.CTRL_MASK));

viewmenu_standard.setMnemonic('T');

viewmenu_science.setMnemonic('S');

viewmenu_grouping.setMnemonic('I');

helpmenu_help.setMnemonic('H');

helpmenu_about.setMnemonic('A');

}

publicvoidinit_pan_back(){

pan_back.setSize(340,40);

pan_back.setLocation(85,60);

pan_back.setLayout(newGridLayout(1,3,5,0));

pan_back.add(btn_back);

pan_back.add(btn_ce);

pan_back.add(btn_c);

btn_back.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

if(!jtf_res.getText().trim().equals("0.")){

if(str.length()!=1&&str.length()!=0){

jtf_res.setText(str.delete(str.length()-1,

str.length()).toString());

}else{

jtf_res.setText("0.");

str.setLength(0);

}

}

op2=newBigDecimal(jtf_res.getText().trim());

}

});

btn_c.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

jtf_res.setText("0.");

op1=op2=newBigDecimal(0);

str.replace(0,str.length(),"");

preOp=currentOp=0;

}

});

btn_ce.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

jtf_res.setText("0.");

}

});

}

publicvoidinit_pan_jtf(){

jtf_res.setHorizontalAlignment(JTextField.RIGHT);

jtf_res.setFont(newFont("仿宋",0,20));

jtf_res.setText("0.");

jtf_res.setEditable(false);

//jtf_res.setFocusable(false);

pan_jtf.setSize(420,40);

pan_jtf.setLocation(10,5);

pan_jtf.setLayout(newBorderLayout());

pan_jtf.add(jtf_res);

}

publicvoidinit_pan_mem(){

pan_mem.setSize(60,270);

pan_mem.setLocation(10,60);

pan_mem.setLayout(newGridLayout(5,1,0,5));

pan_mem.add(btn_mem);

btn_mem.setEnabled(false);

pan_mem.add(btn_mc);

pan_mem.add(btn_mr);

pan_mem.add(btn_ms);

pan_mem.add(btn_m);

}

publicvoidinit_pan_num(){

pan_num.setSize(340,220);

pan_num.setLocation(85,110);

pan_num.setLayout(newGridLayout(4,5,5,5));

pan_num.add(btn_7);

pan_num.add(btn_8);

pan_num.add(btn_9);

pan_num.add(btn_div);

pan_num.add(btn_sqrt);

pan_num.add(btn_4);

pan_num.add(btn_5);

pan_num.add(btn_6);

pan_num.add(btn_multi);

pan_num.add(btn_mod);

pan_num.add(btn_1);

pan_num.add(btn_2);

pan_num.add(btn_3);

pan_num.add(btn_sub);

pan_num.add(btn_x);

pan_num.add(btn_0);

pan_num.add(btn_flag);

pan_num.add(btn_point);

pan_num.add(btn_add);

pan_num.add(btn_res);

btn_1.addActionListener(this);

btn_2.addActionListener(this);

btn_3.addActionListener(this);

btn_4.addActionListener(this);

btn_5.addActionListener(this);

btn_6.addActionListener(this);

btn_7.addActionListener(this);

btn_8.addActionListener(this);

btn_9.addActionListener(this);

btn_0.addActionListener(this);

btn_sub.addActionListener(this);

btn_multi.addActionListener(this);

btn_div.addActionListener(this);

btn_add.addActionListener(this);

btn_sqrt.addActionListener(this);

btn_x.addActionListener(this);

btn_res.addActionListener(this);

btn_point.addActionListener(this);

btn_flag.addActionListener(this);

//btn_mod.addActionListener(this);

}

@Override

publicvoidactionPerformed(ActionEvente){

Strings=e.getActionCommand();

BigDecimali=newBigDecimal(1);

Stringtemp=jtf_res.getText().trim();

BigDecimaltemp1=newBigDecimal(temp);

if(e.getSource()==btn_flag){

BigDecimaltemp3=newBigDecimal(jtf_res.getText());

BigDecimaltemp4=newBigDecimal(-1);

BigDecimaltemp5=temp3.multiply(temp4);

jtf_res.setText(""+temp5);

}elseif(s.equals("1/x")){

jtf_res.setText(""+i.divide(temp1,newMathContext(30)));

}elseif(s.equals("sqrt")){

jtf_res.setText(""+temp1.multiply(temp1));

}elseif(s.equals("+")){

str.setLength(0);

if(currentOp==0){

preOp=currentOp=1;

op2=newBigDecimal("0");

jtf_res.setText(""+op1);

}else{

currentOp=preOp;

preOp=1;

switch(currentOp){

case1:

op1=op1.add(op2);

jtf_res.setText(""+op1);

break;

case2:

op1=op1.subtract(op2);

jtf_res.setText(""+op1);

break;

case3:

op1=op1.multiply(op2);

jtf_res.setText(""+op1);

break;

case4:

op1=op1.divide(op2,newMathContext(30));

jtf_res.setText(""+op1);

break;

}

}

}elseif(s.equals("-")){

str.setLength(0);

if(currentOp==0){

preOp=currentOp=2;

jtf_res.setText(""+op1);

}else{

currentOp=preOp;

preOp=2;

switch(currentOp){

case1:

op1=op1.add(op2);

jtf_res.setText(""+op1);

break;

case2:

op1=op1.subtract(op2);

jtf_res.setText(""+op1);

break;

case3:

op1=op1.multiply(op2);

jtf_res.setText(""+op1);

break;

case4:

op1=op1.divide(op2,newMathContext(30));

jtf_res.setText(""+op1);

break;

}

}

}elseif(s.equals("*")){

str.setLength(0);

if(currentOp==0){

preOp=currentOp=3;

jtf_res.setText(""+op1);

}else{

currentOp=preOp;

preOp=3;

switch(currentOp){

case1:

op1=op1.add(op2);

jtf_res.setText(""+op1);

break;

case2:

op1=op1.subtract(op2);

jtf_res.setText(""+op1);

break;

case3:

op1=op1.multiply(op2);

jtf_res.setText(""+op1);

break;

case4:

op1=op1.divide(op2,newMathContext(30));

jtf_res.setText(""+op1);

break;

}

}

}elseif(s.equals("/")){

str.setLength(0);

if(currentOp==0){

preOp=currentOp=4;

jtf_res.setText(""+op1);

}else{

currentOp=preOp;

preOp=4;

switch(currentOp){

case1:

op1=op1.add(op2);

jtf_res.setText(""+op1);

break;

case2:

op1=op1.subtract(op2);

jtf_res.setText(""+op1);

break;

case3:

op1=op1.multiply(op2);

jtf_res.setText(""+op1);

break;

case4:

op1=op1.divide(op2,newMathContext(30));

jtf_res.setText(""+op1);

break;

}

}

}elseif(s.equals("=")){

if(currentOp==0){

str.setLength(0);

jtf_res.setText(""+op2);

}else{

str.setLength(0);

currentOp=preOp;

switch(currentOp){

case1:

op1=op1.add(op2);

jtf_res.setText(""+op1);

break;

case2:

op1=op1.subtract(op2);

jtf_res.setText(""+op1);

break;

case3:

op1=op1.multiply(op2);

jtf_res.setText(""+op1);

break;

case4:

op1=op1.divide(op2,newMathContext(30));

jtf_res.setText(""+op1);

break;

}

currentOp=0;

op2=newBigDecimal("0");

}

}elseif(s.equals(".")){

isBigDecimal=true;

if(jtf_res.getText().trim().indexOf('.')!=-1)

;

else{

if(jtf_res.getText().trim().equals("0")){

str.setLength(0);

jtf_res.setText((str.append("0"+s)).toString());

}elseif(jtf_res.getText().trim().equals("")){

}else{

jtf_res.setText((str.append(s)).toString());

}

}

}elseif(s.equals("0")){

if(jtf_res.getText().trim().equals("0.")){

}else{

jtf_res.setText(str.append(s).toString());

op2=newBigDecimal(jtf_res.getText().trim());

}

}else{

jtf_res.setText(str.append(s).toString());

op2=newBigDecimal(jtf_res.getText().trim());

if(currentOp==0)

op1=op2;

}

}

}

阅读全文

与java计算器相关的资料

热点内容
手机sql编译软件 浏览:520
外网服务器地址购买 浏览:992
空调压缩机电容价格 浏览:381
小程序选什么云服务器 浏览:656
如何把java编译回中文 浏览:777
天联软件服务器地址是什么 浏览:964
stc单片机加密 浏览:140
小程序地产广告源码 浏览:542
消费者信息加密私域 浏览:431
程序员开发团队可以怎么创业 浏览:925
设备共享服务器是什么意思 浏览:126
java符号类型 浏览:331
redis客户端java 浏览:214
javatn 浏览:278
应用宝哪里下载王卡免流量app 浏览:235
uv7代喷头加密与不加密 浏览:467
滚动指标源码查询 浏览:986
梦幻西游lua源码修改教程 浏览:937
androidphp环境 浏览:762
php前台页面 浏览:493