導航:首頁 > 編程語言 > 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計算器相關的資料

熱點內容
程序員不晉升能幹到多少歲 瀏覽:376
谷歌推廣用什麼伺服器 瀏覽:626
和平精英安卓怎麼登蘋果系統 瀏覽:144
除法的心演算法 瀏覽:300
音樂源碼下載 瀏覽:716
編程拖放 瀏覽:41
linux卸載tomcat 瀏覽:875
手機時間如何校正到伺服器 瀏覽:81
創造與魔法瞬移源碼百度 瀏覽:883
反射優化java 瀏覽:876
硬體加密播放盒子 瀏覽:923
xp點擊文件夾選項沒反應 瀏覽:537
蘋果不顯示桌面的app怎麼刪除 瀏覽:864
安卓手機怎麼換國際服 瀏覽:415
神獸領域安卓怎麼下載 瀏覽:250
單片機交通燈ad原理圖 瀏覽:413
多功能解壓磁鐵筆 瀏覽:80
少兒編程火箭升空 瀏覽:401
蘭斯10游戲解壓碼 瀏覽:42
手機proxy伺服器地址 瀏覽:449