重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java计算加程序代码,java加法运算器代码

JAVA 编写计算器 要代码最简单的

学java的时候自己编的,很简单,能够连续输入计算式后进行计算

创新互联建站是一家专注网站建设、网络营销策划、微信平台小程序开发、电子商务建设、网络推广、移动互联开发、研究、服务为一体的技术型公司。公司成立10余年以来,已经为上千多家成都履带搅拌车各业的企业公司提供互联网服务。现在,服务的上千多家客户与我们一路同行,见证我们的成长;未来,我们一起分享成功的喜悦。

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程序代码,能操作的,简单的计算器有计算功能+-*/就行越简单越好!!!急!!!

public static void main(String[] args) {

String st = "";

计算器 j = new 计算器();

boolean t = true;

while (t) {

System.out.print("请输入计算代码:");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try {

st = br.readLine();

} catch (IOException ex) {

Logger.getLogger(计算器.class.getName()).log(Level.SEVERE, null, ex);

}

if (st.equals("end")) {

t = false;

} else if (!st.equals("")) {

j.处理(st);

}

}

}

public void 处理(String st) {

if (st.indexOf("+") != -1) {

加(符号处理(st, "+"));

} else if (st.indexOf("-") != -1) {

减(符号处理(st, "-"));

} else if (st.indexOf("*") != -1) {

乘(符号处理(st, "*"));

} else if (st.indexOf("/") != -1) {

除(符号处理(st, "/"));

}

}

public int[] 符号处理(String st, String s) {

int[] i = new int[2];

i[0] = Integer.parseInt(st.substring(0, st.indexOf(s)));

i[1] = Integer.parseInt(st.substring(st.indexOf(s) + 1));

return i;

}

public void 加(int x[]) {

System.out.println(x[0] + "+" + x[1] + "=" + (x[0] + x[1]));

}

public void 减(int[] x) {

System.out.println(x[0] + "-" + x[1] + "=" + (x[0] - x[1]));

}

public void 乘(int[] x) {

System.out.println(x[0] + "*" + x[1] + "=" + x[0] * x[1]);

}

public void 除(int x[]) {

if (x[1] == 0) {

System.out.print("除数不能为零");

return;

}

System.out.println(x[0] + "/" + x[1] + "=" + x[0] / x[1]);

}

可以运行:输入示例:55+62

谁能给一个Java的计算器程序代码(加注释 )

以前写的,给你了

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.text.DecimalFormat;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Calculator {

//计算器面板

private JFrame f = new JFrame("Calculator");

//输入面扳

private JPanel inputPanel = new JPanel();

//加减乘除面板

private JPanel operationPanel = new JPanel();

//数字面板

private JPanel buttonsPanel = new JPanel();

//输入数据文本框

private JTextField input = new JTextField(20);

//退格键

private JButton backspace = new JButton("BackSpace");

//清空

private JButton CE = new JButton("CE ");

//删除

private JButton C = new JButton("C ");

//四则运算符号键

private JButton add = new JButton("+");

private JButton sub = new JButton("-");

private JButton mul = new JButton("*");

private JButton div = new JButton("/");

//小数点

private JButton decimal = new JButton(".");

//等号

private JButton equal = new JButton("=");

//数字键

private JButton zero = new JButton("0");

private JButton one = new JButton("1");

private JButton two = new JButton("2");

private JButton three = new JButton("3");

private JButton four = new JButton("4");

private JButton five = new JButton("5");

private JButton six = new JButton("6");

private JButton seven = new JButton("7");

private JButton eight = new JButton("8");

private JButton nine = new JButton("9");

private String num1 = "";//保存第一个运算数字

private String operator = "";//保存运算符号

public static void main(String[] args) {

new Calculator();//new计算器实例

}

public Calculator(){

//添加组件,布局

inputPanel.add(input);

f.add(inputPanel, BorderLayout.NORTH);

operationPanel.add(backspace);

operationPanel.add(CE);

operationPanel.add(C);

f.add(operationPanel, BorderLayout.CENTER);

buttonsPanel.add(add);

buttonsPanel.add(sub);

buttonsPanel.add(mul);

buttonsPanel.add(div);

buttonsPanel.add(one);

buttonsPanel.add(two);

buttonsPanel.add(three);

buttonsPanel.add(zero);

buttonsPanel.add(four);

buttonsPanel.add(five);

buttonsPanel.add(six);

buttonsPanel.add(decimal);

buttonsPanel.add(seven);

buttonsPanel.add(eight);

buttonsPanel.add(nine);

buttonsPanel.add(equal);

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

f.add(buttonsPanel, BorderLayout.SOUTH);

//注册各个组件监听事件

backspace.addMouseListener(new OperationMouseListener());

CE.addMouseListener(new OperationMouseListener());

C.addMouseListener(new OperationMouseListener());

decimal.addMouseListener(new OperationMouseListener());

equal.addMouseListener(new OperationMouseListener());

//注册四则运算监听

add.addMouseListener(new CalcMouseListener());

sub.addMouseListener(new CalcMouseListener());

mul.addMouseListener(new CalcMouseListener());

div.addMouseListener(new CalcMouseListener());

//注册数字监听事件

zero.addMouseListener(new NumberMouseListener());

one.addMouseListener(new NumberMouseListener());

two.addMouseListener(new NumberMouseListener());

three.addMouseListener(new NumberMouseListener());

four.addMouseListener(new NumberMouseListener());

five.addMouseListener(new NumberMouseListener());

six.addMouseListener(new NumberMouseListener());

seven.addMouseListener(new NumberMouseListener());

eight.addMouseListener(new NumberMouseListener());

nine.addMouseListener(new NumberMouseListener());

f.setVisible(true);

f.pack();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private class NumberMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {

if(input.getText().trim().equals("0")){//如果文本框已经是0,结果还是0

input.setText(((JButton)e.getSource()).getText().trim());

}else{//否则的话,把0添加到后面,譬如文本框是1,结果就为10

input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));

}

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

private class CalcMouseListener implements MouseListener{

//如果输入的是运算符号,保存第一个结果和运算符号

public void mouseClicked(MouseEvent e) {

num1 = input.getText().trim();input.setText("");

operator = ((JButton)e.getSource()).getText().trim();

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

private class OperationMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {

if(e.getSource() == backspace){//退格键,删除一个直到没有字符删除

String inputText = input.getText();

if(inputText.length() 0){

input.setText(inputText.substring(0, inputText.length() - 1));

}

}else if(e.getSource() == C){

input.setText("0");//C,清空所有运算数字和符号

num1 = "";

}else if(e.getSource() == CE){

input.setText("0");//CE--将文本框置为0

}else if(e.getSource() == decimal){

String text = input.getText().trim();

//如果按了小数点,如果文本框已经有小数点,不做任何操作,否则在结果后面加上小数点

if(text.indexOf(".") == -1){

input.setText(text.concat("."));

}

}else if(e.getSource() == equal){

//如果是等号

if(!operator.trim().equals("")){

if(!input.getText().trim().equals("")){

double result = 0D;

if(operator.equals("+")){//执行加法运算

result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());

}else if(operator.equals("-")){//减法运算

result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());

}else if(operator.equals("*")){//乘法运算

result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());

}else if(operator.equals("/")){//除法运算

result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());

}

//格式化最终结果,保留两位小数点

input.setText(new DecimalFormat("0.00").format(result));

}

}

}

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

}

用JAVA编写一个程序,计算从1加到50求和,输出结果

1加到50求和的Java代码如下:

public int intSum(){

int total = 0;

for(int i = 1;i51;i ++){

total += i;

}

System.out.println("1加到50结果为:" + total);

return total;

}

结果是:1275

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 [1]  。

Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点 [2]  。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等

急求JAVA简单加法程序代码

写了个简单的,不知道是不是你想要的,里面的main方法作了一个简单的测试。几个变量我定义为类变量。 public class PubTest {

private float fvar1, fvar2;

private int ivar1;

public PubTest(float fval1, float fval2, int ival) {

fvar1 = fval1;

fvar2 = fval2;

ivar1 = ival;

}

public PubTest() {

fvar1 = fvar2 = 0.0f;

ivar1 = 0;

}

public float getFvar1() {

return fvar1;

}

public float getFvar2() {

return fvar2;

}

public int getIvar1() {

return ivar1;

}

public void sum() {

fvar2 += fvar1;

}

public static void main(String[] args) {

PubTest pubTest = new PubTest(3.0f, 5.0f, 1);

pubTest.sum();

System.out.println(pubTest.getFvar2());

}

}


当前标题:java计算加程序代码,java加法运算器代码
文章路径:http://cqcxhl.com/article/dsgohog.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP