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
然後循環獲取() 按照數學運算順序拼起來,
然後把公式拆分一步一步操作就得出結果啦