快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java简便计算器代码 java编写一个简易计算器程序

用java实现一个简单的计算器。

/*

主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、自适应网站建设、程序开发、微网站、微信小程序定制开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的网站制作、成都网站建设、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体,具备承接不同规模与类型的建设项目的能力。

* @(#)JCalculator.java 1.00 06/17/2015

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

* A simple calculator program.

* pI saw this program in a QQ group, and help a friend correct it./p

*

* @author Singyuen Yip

* @version 1.00 12/29/2009

* @see JFrame

* @see ActionListener

*/

public class JCalculator extends JFrame implements ActionListener {

/**

* Serial Version UID

*/

private static final long serialVersionUID = -169068472193786457L;

/**

* This class help close the Window.

* @author Singyuen Yip

*

*/

private class WindowCloser extends WindowAdapter {

public void windowClosing(WindowEvent we) {

System.exit(0);

}

}

int i;

// Strings for Digit Operator buttons.

private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*","1",

"2", "3", "-", ".", "0", "=", "+" };

// Build buttons.

JButton[] buttons = new JButton[str.length];

// For cancel or reset.

JButton reset = new JButton("CE");

// Build the text field to show the result.

JTextField display = new JTextField("0");

/**

* Constructor without parameters.

*/

public JCalculator() {

super("Calculator");

// Add a panel.

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);

// Add action listener for each digit operator button.

for (i = 0; i  str.length; i++)

buttons[i].addActionListener(this);

// Add listener for "reset" button.

reset.addActionListener(this);

// Add listener for "display" button.

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 ("0123456789.".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 = "=";

/**

* Handling the operation.

* @param key pressed operator's key.

*/

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计算器代码,

public static void main(String[] args) {

System.out.println("简单计算器");

boolean flag=true;//while循环是否继续,true继续循环,false停止循环

System.out.println("请输入第一个数字");

while(flag){

Scanner scan = new Scanner(System.in);

String bh=scan.next();

try {

double num = Double.parseDouble(bh);

System.out.println("请输入符号(+、-、*、/)");

while(1==1){

String fh=scan.next();

if("+".equals(fh) || "-".equals(fh) || "*".equals(fh) || "/".equals(fh)){

System.out.println("请输入第二个数字");

while(1==1){

String bh2=scan.next();

try {

double num2 = Double.parseDouble(bh2);

double num3=0;

if("+".equals(fh)){

num3=num+num2;

}else if("-".equals(fh)){

num3=num-num2;

}else if("*".equals(fh)){

num3=num*num2;

}else if("/".equals(fh)){

num3=num/num2;

}

System.out.println(num3);

break;

} catch (Exception e) {

System.out.println("请输入第二个正确的数字");

continue;

}

}

break;

}else{

System.out.println("请输入正确的符号(+、-、*、/)");

continue;

}

}

System.out.println("是否继续运算:输入Y或者y继续,输入其它任意字符退出");

String yn=scan.next();

if("Y".equals(yn) || "y".equals(yn)){

continue;

}else{

System.out.println("运算结束");

break;

}

}catch (NumberFormatException e) {//输入非数字类型时

System.out.println("请输入第一个正确的数字");

continue;

}

}

}

求JAVA简易计算器代码。

import javax.swing.SwingUtilities;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JTextField;

import java.awt.Rectangle;

import javax.swing.JButton;

public class Calculator extends JFrame {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JTextField jTextField = null;

private JButton jButton = null;

private JButton jButton1 = null;

private JButton jButton2 = null;

private JButton jButton3 = null;

private JButton jButton4 = null;

private JButton jButton5 = null;

private JButton jButton6 = null;

private JButton jButton7 = null;

private JButton jButton8 = null;

private JButton jButton9 = null;

private JButton jButton10 = null;

private JButton jButton11 = null;

private JButton jButton12 = null;

private JButton jButton13 = null;

private JButton jButton14 = null;

private double first = 0;

private double second = 0;

private JButton jButton15 = null;

private JButton jButton16 = null;

private String way = "";

private double result = 0;

private char[] ch = new char[12];

private int i = 0;

/**

* This method initializes jTextField

*

* @return javax.swing.JTextField

*/

private JTextField getJTextField() {

if (jTextField == null) {

jTextField = new JTextField("0");

jTextField.setBounds(new Rectangle(60, 15, 234, 31));

jTextField.setHorizontalAlignment(JTextField.RIGHT);

}

return jTextField;

}

/**

* This method initializes jButton

*

* @return javax.swing.JButton

*/

private JButton getJButton() {

if (jButton == null) {

jButton = new JButton();

jButton.setBounds(new Rectangle(62, 74, 44, 37));

jButton.setText("1");

jButton.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

ch[i] = '1';

jTextField.setText(new String(ch).trim());

i++;

}

});

}

return jButton;

}

/**

* This method initializes jButton1

*

* @return javax.swing.JButton

*/

private JButton getJButton1() {

if (jButton1 == null) {

jButton1 = new JButton();

jButton1.setBounds(new Rectangle(119, 74, 44, 37));

jButton1.setText("2");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

ch[i] = '2';

jTextField.setText(new String(ch).trim());

i++;

}

});

}

return jButton1;

}

/**

* This method initializes jButton2

*

* @return javax.swing.JButton

*/

private JButton getJButton2() {

if (jButton2 == null) {

jButton2 = new JButton();

jButton2.setBounds(new Rectangle(176, 74, 44, 37));

jButton2.setText("3");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

ch[i] = '3';

jTextField.setText(new String(ch).trim());

i++;

}

});

}

return jButton2;

}

/**

* This method initializes jButton3

*

* @return javax.swing.JButton

*/

private JButton getJButton3() {

if (jButton3 == null) {

jButton3 = new JButton();

jButton3.setBounds(new Rectangle(62, 129, 44, 37));

jButton3.setText("4");

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

ch[i] = '4';

jTextField.setText(new String(ch).trim());

i++;

}

});

}

return jButton3;

}

/**

* This method initializes jButton4

*

* @return javax.swing.JButton

*/

private JButton getJButton4() {

if (jButton4 == null) {

jButton4 = new JButton();

jButton4.setBounds(new Rectangle(119, 129, 44, 37));

jButton4.setText("5");

jButton4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

ch[i] = '5';

jTextField.setText(new String(ch).trim());

i++;

}

});

}

return jButton4;

}

/**

* This method initializes jButton5

*

* @return javax.swing.JButton

*/

private JButton getJButton5() {

if (jButton5 == null) {

jButton5 = new JButton();

jButton5.setBounds(new Rectangle(176, 129, 44, 37));

jButton5.setText("6");

jButton5.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

ch[i] = '6';

jTextField.setText(new String(ch).trim());

i++;

}

});

}

return jButton5;

}

/**

* This method initializes jButton6

*

* @return javax.swing.JButton

*/

private JButton getJButton6() {

if (jButton6 == null) {

jButton6 = new JButton();

jButton6.setBounds(new Rectangle(62, 184, 44, 37));

jButton6.setText("7");

jButton6.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

ch[i] = '7';

jTextField.setText(new String(ch).trim());

i++;

}

});

}

return jButton6;

}

/**

* This method initializes jButton7

*

* @return javax.swing.JButton

*/

private JButton getJButton7() {

if (jButton7 == null) {

jButton7 = new JButton();

jButton7.setBounds(new Rectangle(119, 184, 44, 37));

jButton7.setText("8");

jButton7.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

ch[i] = '8';

jTextField.setText(new String(ch).trim());

i++;

}

});

}

return jButton7;

}

/**

* This method initializes jButton8

*

* @return javax.swing.JButton

*/

private JButton getJButton8() {

if (jButton8 == null) {

jButton8 = new JButton();

jButton8.setBounds(new Rectangle(176, 184, 44, 37));

jButton8.setText("9");

jButton8.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

ch[i] = '9';

jTextField.setText(new String(ch).trim());

i++;

}

});

}

return jButton8;

}

/**

* This method initializes jButton9

*

* @return javax.swing.JButton

*/

private JButton getJButton9() {

if (jButton9 == null) {

jButton9 = new JButton();

jButton9.setBounds(new Rectangle(233, 74, 44, 37));

jButton9.setText("+");

jButton9.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

first = Double.parseDouble(jTextField.getText());

way = "+";

ch = new char[12];

i = 0;

jButton16.setEnabled(true);

}

});

}

return jButton9;

}

/**

* This method initializes jButton10

*

* @return javax.swing.JButton

*/

private JButton getJButton10() {

if (jButton10 == null) {

jButton10 = new JButton();

jButton10.setBounds(new Rectangle(290, 74, 44, 37));

jButton10.setText("-");

jButton10.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

first = Double.parseDouble(jTextField.getText());

way = "-";

ch = new char[12];

i = 0;

jButton16.setEnabled(true);

}

});

}

return jButton10;

}

/**

* This method initializes jButton11

*

* @return javax.swing.JButton

*/

private JButton getJButton11() {

if (jButton11 == null) {

jButton11 = new JButton();

jButton11.setBounds(new Rectangle(233, 129, 44, 37));

jButton11.setText("*");

jButton11.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

first = Double.parseDouble(jTextField.getText());

way = "*";

ch = new char[12];

i = 0;

jButton16.setEnabled(true);

}

});

}

return jButton11;

}

/**

* This method initializes jButton12

*

* @return javax.swing.JButton

*/

private JButton getJButton12() {

if (jButton12 == null) {

jButton12 = new JButton();

jButton12.setBounds(new Rectangle(290, 129, 44, 37));

jButton12.setText("/");

jButton12.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

first = Double.parseDouble(jTextField.getText());

way = "/";

ch = new char[12];

i = 0;

jButton16.setEnabled(true);

}

});

}

return jButton12;

}

/**

* This method initializes jButton13

*

* @return javax.swing.JButton

*/

private JButton getJButton13() {

if (jButton13 == null) {

jButton13 = new JButton();

jButton13.setBounds(new Rectangle(233, 184, 44, 37));

jButton13.setText("=");

jButton13.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

second = Double.parseDouble(jTextField.getText());

if (way.equals("")){

jTextField.setText(""+second);

}

if (way.equals("+")){

result = first + second;

jTextField.setText(""+result);

}

if (way.equals("-")){

result = first - second;

jTextField.setText(""+result);

}

if (way.equals("*")){

result = first * second;

jTextField.setText(""+result);

}

if(way.equals("/")){

if (second != 0){

result = first / second;

jTextField.setText(""+result);

}else{

jTextField.setText("error");

}

}

jButton16.setEnabled(true);

}

});

}

return jButton13;

}

/**

* This method initializes jButton14

*

* @return javax.swing.JButton

*/

private JButton getJButton14() {

if (jButton14 == null) {

jButton14 = new JButton();

jButton14.setBounds(new Rectangle(290, 184, 44, 37));

jButton14.setText("C");

jButton14.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

jTextField.setText("0");

first = 0;

second = 0;

way = "";

ch = new char[12];

i = 0;

jButton16.setEnabled(true);

}

});

}

return jButton14;

}

/**

* This method initializes jButton15

*

* @return javax.swing.JButton

*/

private JButton getJButton15() {

if (jButton15 == null) {

jButton15 = new JButton();

jButton15.setBounds(new Rectangle(62, 231, 102, 26));

jButton15.setText("0");

jButton15.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

ch[i] = '0';

jTextField.setText(new String(ch).trim());

i++;

}

});

}

return jButton15;

}

/**

* This method initializes jButton16

*

* @return javax.swing.JButton

*/

private JButton getJButton16() {

if (jButton16 == null) {

jButton16 = new JButton();

jButton16.setBounds(new Rectangle(176, 231, 44, 26));

jButton16.setText(".");

jButton16.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

if(ch[0] == 0){

ch[i] = '0';

ch[i+1] = '.';

i++;

}else{

ch[i] = '.';

}

jTextField.setText(new String(ch).trim());

i++;

jButton16.setEnabled(false);

}

});

}

return jButton16;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

SwingUtilities.invokeLater(new Runnable() {

public void run() {

Calculator thisClass = new Calculator();

thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

thisClass.setVisible(true);

}

});

}

/**

* This is the default constructor

*/

public Calculator() {

super();

initialize();

}

/**

* This method initializes this

*

* @return void

*/

private void initialize() {

this.setSize(356, 298);

this.setContentPane(getJContentPane());

this.setTitle("计算器");

this.setResizable(false);

}

/**

* This method initializes jContentPane

*

* @return javax.swing.JPanel

*/

private JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(null);

jContentPane.add(getJTextField(), null);

jContentPane.add(getJButton(), null);

jContentPane.add(getJButton1(), null);

jContentPane.add(getJButton2(), null);

jContentPane.add(getJButton3(), null);

jContentPane.add(getJButton4(), null);

jContentPane.add(getJButton5(), null);

jContentPane.add(getJButton6(), null);

jContentPane.add(getJButton7(), null);

jContentPane.add(getJButton8(), null);

jContentPane.add(getJButton9(), null);

jContentPane.add(getJButton10(), null);

jContentPane.add(getJButton11(), null);

jContentPane.add(getJButton12(), null);

jContentPane.add(getJButton13(), null);

jContentPane.add(getJButton14(), null);

jContentPane.add(getJButton15(), null);

jContentPane.add(getJButton16(), null);

}

return jContentPane;

}

} // @jve:decl-index=0:visual-constraint="437,27"

哥们!这是用eclipse中的visual editor做的,所以代码比较多,复制粘贴不方便,我可以发你邮箱。你把代码打上去,一样能运行的!!!

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.=/";

ArrayListString arrayList = new ArrayListString();//保存计算式,通过方法进行运算

public CalculatorA(){

frame = new JFrame("我的计算器v1.1");

frame.setLocation(300,300);

field = new JTextField(25);

allButtons = new JButton[16];

for(int i=0;iallButtons.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;iallButtons.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;iallButtons.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 ArrayListString getResult(ArrayListString list){

String res = null;

String[] s = {"/","*","-","+"};

int i=0;

if(list.size()1){

for(;is.length;){

if(s[i].equals("/")){

for(int j=0;jlist.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;jlist.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;jlist.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;jlist.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;kc.length;k++){

c[k] = res.charAt(k);

}

for(int k=0;kc.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简便计算器代码 java编写一个简易计算器程序
URL网址:http://6mz.cn/article/hjsjog.html

其他资讯