java计算器流程图_帮帮忙:Java小计算器代码,及需求分析.流程图.
展開全部
package example;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame {
/**
* 本實例實現功能如下 1.普通加減乘除運算 2.小數點的情況已經解32313133353236313431303231363533e58685e5aeb931333236393738決 3.開始按0已經解決 4.消去鍵可以起作用
*
*/
private static final long serialVersionUID = 1L;
private String name[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
".", "=", "+", "-", "*", "/", "Backspace", "", "", "C" };
private Button button[] = new Button[name.length];
private TextField textfield = new TextField("0.");
// 設置2個字符A1,A2用于存放點擊運算符號之前的String數據
private String A1 = null, A2 = null;
// 設置2個字符B1,B2用于存放點擊運算符號之后的String數據
private String B1 = null, B2 = null;
// 存放運算符號前后的數據,douuble類型進行運算
private double A, B;
// s存放為哪種運算符號,Result存放最后的運行結果
private String Result="0", s;
// 判斷這個數字是否為小數,小數的時為true不是時為false
private boolean Decimal=false;
// 構造器,顯示在標題欄
public Calculator() {
super("TEST:Caculator");
}
// 計算器的基本布局,在一個BorderLayout上面放置了一個GridLayout一個BorderLayout
public void init() {
setLayout(new BorderLayout(2, 2));
// 設置2個Panel
Panel p0 = new Panel();
Panel p1 = new Panel();
// p0上添加所有按扭
p0.setLayout(new GridLayout(5, 4, 1, 1));
// 不同的按扭采用不同的監聽事件0-9和"."采用ButtonListener()
for (int i = 0; i < 11; i++) {
button = new Button(name);
// 設置字體顏色為藍色
button.setForeground(Color.blue);
p0.add(button);
button.addActionListener(new ButtonListener());
}
// 其余的運算符號采取ButtonListener2()另一監聽事件
for (int i = 11; i < name.length; i++) {
button = new Button(name);
// 設置字體顏色為紅色
button.setForeground(Color.RED);
p0.add(button);
button.addActionListener(new ButtonListener2());
}
// 設置中間2個按扭不可見,增加美觀,對稱
button[17].setVisible(false);
button[18].setVisible(false);
// p1上添加文本輸出區域
p1.setLayout(new BorderLayout(5, 5));
p1.add(textfield);
// 不可以向文本框里隨便寫東西
textfield.setEditable(false);
// 設置文本框背景為白色
textfield.setBackground(Color.WHITE);
add(p0, BorderLayout.SOUTH);
add(p1, BorderLayout.NORTH);
pack();
// 設置打開窗口在顯示器的中間顯示
setLocationRelativeTo(null);
// 關閉按扭,適配器模式
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 調整可見
setVisible(true);
}
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.print(e.getActionCommand() + " ");
// 當A2和B2同時為null時候表示程序第一次運行或者開始一次新的運行,即按"="之后,將按鍵獲取的值賦給A2--setp1
if (A2 == null && B2 == null) {
// 所按的數字按扭不是"0"的時候對A2進行賦值,否則給A2值不變,但是讓textfield恢復初始值"0."
if (!"0".equals(e.getActionCommand())) {
// 考慮第一次按小數點的情況,按小數點后將boolean類型的Decimal定義為true
if (".".equals(e.getActionCommand())) {
A2 = "0.";
Decimal = true;
textfield.setText(A2);
} else {
A2 = e.getActionCommand();
textfield.setText(A2);
}
} else {
if ("0".equals(e.getActionCommand())) {
} else {
A2 = e.getActionCommand();
textfield.setText("0");
}
}
// 當A2不等于null,B2和A為null,表示還沒有按運算符號,仍然對A2進行賦值
} else if (A2 != null && A1 == null && B2 == null) {
// 已經是小數的在點小數點不做任何動作,不是小數的在按"."的時候追加賦值并且設置Decimal為true
if (".".equals(e.getActionCommand())) {
if (Decimal) {
} else {
Decimal = true;
A2 += e.getActionCommand();
textfield.setText(A2);
}
} else {
A2 += e.getActionCommand();
textfield.setText(A2);
}
// 當A,A2不等于null,B2為null,表示剛按過運算符號,開始對B2進行賦值
// 仍要考慮"0"的情況,但要考慮0做為被減數,乘數和加數,此處B2可以等于0
// B2也可以是小數將Decimal設置回false
} else if (A2 != null && A1 != null && B2 == null) {
Decimal = false;
if (!"0".equals(e.getActionCommand())) {
// 不為"0"但為"."的情況,原理同上
if (".".equals(e.getActionCommand())) {
Decimal = true;
if (Decimal) {
B2 = "0.";
} else {
Decimal = true;
B2 = e.getActionCommand();
textfield.setText(B2);
}
} else {
B2 = e.getActionCommand();
textfield.setText(B2);
}
} else {
B2 = "0";
textfield.setText(B2);
}
// 當A,A2,B2都不為null的時候表示B2已經被賦值,繼續按數字按扭對B2繼續賦值
// 當B2等于0的時候不進行追加直接賦值,僅當B2不等于0的時候才進行追加
} else if (A2 != null && A1 != null && B2 != null) {
if ("0".equals(B2)) {
// 考慮用戶連續2次按"0"的情況
if ("0".equals(e.getActionCommand())) {
B2 = "0";
textfield.setText(B2);
} else {
B2 = e.getActionCommand();
textfield.setText(B2);
}
} else {
// 不為"0"但為"."的情況,原理同上
if (".".equals(e.getActionCommand())) {
if (Decimal) {
} else {
Decimal = true;
B2 += e.getActionCommand();
textfield.setText(B2);
}
} else {
B2 += e.getActionCommand();
textfield.setText(B2);
}
}
}
}
}
public class ButtonListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.print(e.getActionCommand() + " ");
// 一旦按了運算符號"+-*/"A2賦值給A,使得A和A2都不為null,但此時B2還為null在按數字鍵的時候便會給B2賦值
// 給s--運算符號賦值
if ("+".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "+";
} else if ("-".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "-";
} else if ("*".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "*";
} else if ("/".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "/";
// 等號的時候把B2賦值給B,進行最后的運算
} else if ("=".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
if (B2 == null) {
B2 = "0";
}
A1 = A2;
B1 = B2;
// 將String類型轉換為double進行運算
A = Double.parseDouble(A1);
B = Double.parseDouble(B1);
if ("+".equals(s)) {
Result = String.valueOf(A + B);
System.out.println();
System.out.println(A + "+" + B + "=" + Result);
} else if ("-".equals(s)) {
Result = String.valueOf(A - B);
System.out.println();
System.out.println(A + "-" + B + "=" + Result);
} else if ("*".equals(s)) {
Result = String.valueOf(A * B);
System.out.println();
System.out.println(A + "*" + B + "=" + Result);
}
textfield.setText(Result);
A1 = null;
A2 = null;
B1 = null;
B2 = null;
Decimal=false;
} else if ("Backspace".equals(e.getActionCommand())) {
String text = "0";
if (A2 == null && A1 == null) {
} else if (A2 != null && A1 == null && B2 == null) {
int t = A2.length();
text = A2;
if (t == 1) {
text = "0";
} else {
text = text.substring(0, t - 1);
}
A2 = text;
} else if (A2 != null && A1 != null && B2 == null) {
} else {
int t = B2.length();
text = B2;
if (t == 1) {
text = "0";
} else {
text = text.substring(0, t - 1);
}
B2 = text;
}
textfield.setText(text);
System.out.println();
System.out.println("text=" + text + " ");
System.out.println(A1 + ":" + A2 + ":" + B1 + ":" + B2);
// 選擇"C"的時候將計算器重置,即恢復到開始的狀態
} else if ("C".equals(e.getActionCommand())) {
textfield.setText("0.");
A1 = null;
A2 = null;
B1 = null;
B2 = null;
Decimal=false;
}
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.init();
}
}
已贊過
已踩過<
你對這個回答的評價是?
評論
收起
總結
以上是生活随笔為你收集整理的java计算器流程图_帮帮忙:Java小计算器代码,及需求分析.流程图.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pyqt 取鼠标处文字_爱剪辑:炫彩的动
- 下一篇: python redis模块常用_pyt