java怎么做个简单的计算器

你是要这个吗?兄弟
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame {

private Container container;

private GridBagLayout layout;

private GridBagConstraints constraints;

private JTextField displayField;// 计算结果显示区

private String lastCommand;// 保存+,-,*,/,=命令

private double result;// 保存计算结果

private boolean start;// 判断是否为数字的开始

public Calculator() {

super("Calculator");
container = getContentPane();
layout = new GridBagLayout();
container.setLayout(layout);
constraints = new GridBagConstraints();
start = true;
result = 0;
lastCommand = "=";
displayField = new JTextField(20);
displayField.setHorizontalAlignment(JTextField.RIGHT);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 4;
constraints.gridheight = 1;
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 100;
constraints.weighty = 100;
layout.setConstraints(displayField, constraints);
container.add(displayField);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
// addButton("Backspace", 0, 1, 2, 1, insert);
// addButton("CE", 2, 1, 1, 1, insert);
// addButton("C", 3, 1, 1, 1, insert);
addButton("7", 0, 2, 1, 1, insert);
addButton("8", 1, 2, 1, 1, insert);
addButton("9", 2, 2, 1, 1, insert);
addButton("/", 3, 2, 1, 1, command);
addButt on("4", 0, 3, 1, 1, insert);
addButton("5", 1, 3, 1, 1, insert);
addButton("6", 2, 3, 1, 1, insert);
addButton("*", 3, 3, 1, 1, command);
addButton("1", 0, 4, 1, 1, insert);
addButton("2", 1, 4, 1, 1, insert);
addButton("3", 2, 4, 1, 1, insert);
addButton("-", 3, 4, 1, 1, command);
addButton("0", 0, 5, 1, 1, insert);
// addButton("+/-", 1, 5, 1, 1, insert);// 只显示"-"号,"+"没有实用价值
addButton(".", 2, 5, 1, 1, insert);
addButton("+", 3, 5, 1, 1, command);
addButton("=", 0, 6, 4, 1, command);
this.setResizable(false);
setSize(180, 200);
setVisible(true);

}

private void addButton(String label, int row, int column, int with,
int height, ActionListener listener) {

JButton button = new JButton(label);
constraints.gridx = row;
constraints.gridy = column;
constraints.gridwidth = with;
constraints.gridheight = height;
constraints.fill = GridBagConstraints.BOTH;
button.addActionListener(listener);
layout.setConstraints(button, constraints);
container.add(button);
}

private class InsertAction implements ActionListener {

public void actionPerformed(ActionEvent event) {

String input = event.getActionCommand();
if (start) {
displayField.setText("");
start = false;
if (input.equals("+/-"))
displayField.setText(displayField.getText() + "-");
}

if (!input.equals("+/-")) {
if (input.equals("Backspace")) {
String str = displayField.getText();
if (str.length() > 0)
displayField
.setText(str.substring(0, str.length() - 1));
} else if (input.equals("CE") || input.equals("C")) {
displayField.setText("0");
start = true;
} else
displayField.setText(displayField.getText() + input);
}
}
}

private class CommandAction implements ActionListener {

public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
if (start) {
lastCommand = command;
} else {
calculate(Double.parseDouble(displayField.getText()));
lastCommand = command;
start = true;
}
}
}

public void calculate(double x) {
if (lastCommand.equals("+"))
result += x;
else if (lastCommand.equals("-"))
result -= x;
else if (lastCommand.equals("*"))
result *= x;
else if (lastCommand.equals("/"))
result /= x;
else if (lastCommand.equals("="))
result = x;
displayField.setText("" + result);
}

public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

⑵ 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编写一个简单的计算器 要求如下

我倒是能用android做一个,逻辑很简单,但是java界面不会做啊

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

说实在的,你写的这个计算器不怎么样。特别是布局。
我给你一个很简单的吧。你自己学习一下。。

import java.awt.*;
import java.awt.event.*;
/**
*
* @author zzj
*
*/
public class Jisuanqi extends WindowAdapter {
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
TextField txt;
private Button[] b = new Button[17];
private String ss[] = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2",
"3", "*", "清空", "0", "=", "/", "关闭" };
static double a;
static String s, str;//定义变量 创建对像

public static void main(String args[]) {
(new Jisuanqi()).frame();
}

public void frame() {
Frame fm = new Frame("简单计算器");
for (int i = 0; i <= 16; i++) {
b[i] = new Button(ss[i]);
}
for (int i = 0; i <= 15; i++) {
p2.add(b[i]);
} //创建按钮 并添加到P2
b[16].setBackground(Color.yellow);
txt = new TextField(15);
txt.setEditable(false);
for (int i = 0; i <= 16; i++) {
b[i].addActionListener(new buttonlistener());//添加监听器
}
b[16].addActionListener(new close());
fm.addWindowListener(this);
fm.setBackground(Color.red);
p1.setLayout(new BorderLayout());
p1.add(txt, "North");
p2.setLayout(new GridLayout(4, 4));
p3.setLayout(new BorderLayout());
p3.add(b[16]);
fm.add(p1, "North");
fm.add(p2, "Center");
fm.add(p3, "South");
fm.pack();
fm.setVisible(true);//都是些窗中设置 添加相关组件和监听器
}

public void windowClosing(WindowEvent e) {
System.exit(0);//退出系统
}

class buttonlistener implements ActionListener {//编写监听器事件 通过按键得出给果
public void actionPerformed(ActionEvent e) {
Button btn = (Button) e.getSource();
if (btn.getLabel() == "=") {
jisuan();
str = String.valueOf(a);
txt.setText(str);
s = "";
} else if (btn.getLabel() == "+") {
jisuan();
txt.setText("");
s = "+";
} else if (btn.getLabel() == "-") {
jisuan();
txt.setText("");
s = "-";
} else if (btn.getLabel() == "/") {
jisuan();
txt.setText("");
s = "/";

} else if (btn.getLabel() == "*") {
jisuan();
txt.setText("");
s = "*";
} else {
txt.setText(txt.getText() + btn.getLabel());

if (btn.getLabel() == "清空")
txt.setText("");
}
}

public void jisuan() {//编写具体计算方法
if (s == "+")
a += Double.parseDouble(txt.getText());
else if (s == "-")
a -= Double.parseDouble(txt.getText());
else if (s == "*")
a *= Double.parseDouble(txt.getText());
else if (s == "/")
a /= Double.parseDouble(txt.getText());
else
a = Double.parseDouble(txt.getText());
}
}
}

class close implements ActionListener {//退出
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}

⑸ 怎么用java做个简单的计算器

package com.gjq.test;

import java.util.Scanner;

public class xte {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println("****************计算器****************");

System.out.print("请输入数字①:");
Scanner scanner1 = new Scanner(System.in);
double num1= scanner1.nextDouble();

System.out.print("请输入运算符,如:+ - * /:");
Scanner scanner2 = new Scanner(System.in);
String zifu= scanner2.next();

System.out.print("请输入数字②:");
Scanner scanner3 = new Scanner(System.in);
double num3= scanner3.nextDouble();

char charzf =zifu.toCharArray()[0];
switch (charzf) {
case '+':
System.out.println("结果为:"+num1+" "+charzf+" "+num3+" = "+(num1+num3));
break;
case '-':
System.out.println("结果为:"+num1+" "+charzf+" "+num3+" = "+(num1-num3));
break;
case '*':
System.out.println("结果为:"+num1+" "+charzf+" "+num3+" = "+(num1*num3));
break;
case '/':
if(num3!=0){
System.out.println("结果为:"+num1+" "+charzf+" "+num3+" = "+(num1/num3));
}
break;
default:
System.out.println("输入错误!");
break;
}

}

}

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

import java.awt.*;
import java.awt.event.*;

public class CalcAppDemo extends Frame{
private TextField t_result;
private Panel p_main; //主面板
private Panel p_num; //数字面板
private Panel p_oper; //操作符面板
private Panel p_show; //显示面板
private Button b_num[]; //数字按钮
private Button b_oper[]; //操作符面板

public CalcAppDemo(String title){
setTitle(title);
t_result = new TextField("0.0", 21);
p_main = new Panel();
p_num = new Panel();
p_oper = new Panel();
p_show = new Panel();
p_main.setLayout(new BorderLayout());
p_num.setLayout(new GridLayout(4, 3, 1, 1));
p_oper.setLayout(new GridLayout(4, 2, 1, 1));

b_num = new Button[12];
for(int i=0; i<9; i++)
{
b_num[i] = new Button(new Integer(i+1).toString());
}
b_num[9] = new Button("0");
b_num[10] = new Button("cls");
b_num[11] = new Button(".");
for(int i=0; i<12; i++)
{
p_num.add(b_num[i]);
}

b_oper = new Button[8];
b_oper[0] = new Button("+");
b_oper[1] = new Button("-");
b_oper[2] = new Button("*");
b_oper[3] = new Button("/");
b_oper[4] = new Button("pow");
b_oper[5] = new Button("sqrt");
b_oper[6] = new Button("+/-");
b_oper[7] = new Button("=");
for(int i=0; i<8; i++) //
{
p_oper.add(b_oper[i]);
}

t_result.setEditable(false);
p_show.add(t_result, BorderLayout.NORTH);
p_main.add(p_show, BorderLayout.NORTH);
p_main.add(p_num, BorderLayout.WEST);
p_main.add(p_oper, BorderLayout.EAST);
this.add(p_main, BorderLayout.CENTER);
setSize(400, 400);
setResizable(false);
pack();

this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
ButtonListener b1 = new ButtonListener();
for(int i=0; i<12; i++)
{
b_num[i].addActionListener(b1);
}
for(int i=0; i<8; i++)
{
b_oper[i].addActionListener(b1);
}
}

class ButtonListener implements ActionListener
{
private String lastOp; //存储上一此操作符
private String strVal; //存储数字对应的字符串
private double total; //总数
private double number; //存储新输入的数
private boolean firsttime; //判断是否第一次按下的是操作符按钮
private boolean operatorPressed;//判断是否已经按过操作符按钮

ButtonListener()
{
firsttime = true;
strVal = "";
}

//事件处理器
public void actionPerformed(ActionEvent e)
{
String s = ((Button)e.getSource()).getLabel().trim();

if(Character.isDigit(s.charAt(0)))
{//判断是操作数还是操作符
handleNumber(s);
}
else
{
calculate(s);
}
}

//判断是一元操作符还是二元操作符,并根据操作符类型做计算
void calculate(String op)
{
operatorPressed = true;

if(firsttime&&! isUnary(op))
{
total = getNumberOnDisplay();
firsttime = false;
}
if(isUnary(op))
{
handleUnaryOp(op);
}
else if(lastOp != null)
{
handleBinaryOp(lastOp);
}
if(! isUnary(op))
{
lastOp = op;
}
}

//判断是否一元操作符
boolean isUnary(String s)
{
return s.equals("=")
||s.equals("cls")||s.equals("sqrt")
||s.equals("+/-")||s.equals(".");
}

//处理一元操作符
void handleUnaryOp(String op)
{
if(op.equals("+/-"))
{//
number = negate(getNumberOnDisplay() + "");
t_result.setText("");
t_result.setText(number + "");
return;
}else if(op.equals("."))
{
handleDecPoint();
return;
}else if(op.equals("sqrt"))
{
number = Math.sqrt(getNumberOnDisplay());
t_result.setText("");
t_result.setText(number + "");
return;
}else if(op.equals("="))
{//
if(lastOp!= null && !isUnary(lastOp))
{
handleBinaryOp(lastOp);
}
lastOp = null;
firsttime = true;
return;
}else
{
clear();
}
}

//处理二元运算符
void handleBinaryOp(String op)
{
if(op.equals("+"))
{
total +=number;
}else if(op.equals("-"))
{
total -=number;
}else if(op.equals("*"))
{
total *=number;
}else if(op.equals("/"))
{
try
{
total /=number;
}catch(ArithmeticException ae){}
}else if(op.equals("pow"))
total = Math.pow(total, number);
//t_result.setText("");
lastOp = null;
// strVal = "";
number = 0;
t_result.setText(total + "");
}

//该方法用于处理数字按钮
void handleNumber(String s)
{
if(!operatorPressed)
{
strVal += s;
}else
{
operatorPressed = false;
strVal = s;
}
//
number = new Double(strVal).doubleValue();
t_result.setText("");
t_result.setText(strVal);
}

//该方法用于按下"."按钮
void handleDecPoint()
{
operatorPressed = false;
//
if(strVal.indexOf(".")<0)
{
strVal += ".";
}
t_result.setText("");
t_result.setText(strVal);
}

//该方法用于将一个数求反
double negate(String s)
{
operatorPressed = false;
//如果是一个整数,去掉小数点后面的0
if(number == (int)number)
{
s = s.substring(0,s.indexOf("."));
}

//如果无"-"增加在该数的前面
if(s.indexOf("-")<0)
{
strVal = "-" + s;
}
else
{
strVal = s.substring(1);
}
return new Double(strVal).doubleValue();
}

//将显示框中的值转换成Double
double getNumberOnDisplay()
{
return new Double(t_result.getText()).doubleValue();
}

//清除屏幕并设置所有的标识
void clear()
{
firsttime = true;
lastOp = null;
strVal = "";
total = 0;
number = 0;
t_result.setText("0");
}
}

public static void main(String[] args) {
CalcAppDemo c = new CalcAppDemo("简单的计算器程序");
c.setVisible(true);
}

}

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

太麻烦了 说个思路, 过去字符串后 先获取+-*%的下标.然后然后分割,获取到一个数组或list
然后循环获取() 按照数学运算顺序拼起来,
然后把公式拆分一步一步操作就得出结果啦