用java怎麼編寫簡易計算器?有哪幾種編寫方法呢?下面跟yjbys小編一起來看看吧!
界面設計:用一個JPanel1 佈局是網格 (4*4),分別放置數字和功能鍵,再用一個JPanel2 放置顯示區(JTextArea)1和2,在把JPanel1放入JPanel2中,JPanel2 放入JFrame中;
計算:
有數字按鈕按下時,把相應的值讀入顯示區裏,當有功能鍵按下時,顯示區清空,在讀入第二操作數,當有”=”按下時,調用相應的函數計算結果,並顯示。並激活相應的'探聽器。
方法1:
import .*;
import t.*;
public class MyCalculator {
private Frame f;
private TextField tf = new TextField(30);
private long result;
private boolean append=false;
private char operator='=';
private Button[] btn=new Button[15];
public MyCalculator() {
initComponent();
}
private void initComponent() {
f = new Frame("My Calculator V1.0");
ayout(new BorderLayout()); //The frame uses BorderLayout
indowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
(0);
}
});
Panel centerPanel = new Panel();
ayout(new GridLayout(5, 3)); //The panel uses GridLayout
NumberListener nl=new NumberListener();
OperatorListener ol=new OperatorListener();
btn[10]=new Button("+");
btn[11]=new Button("-");
btn[12]=new Button("*");
btn[13]=new Button("/");
btn[14]=new Button("=");
for (int i=0;i<=9;i++){
btn[i]=new Button(eOf(i));
(btn[i]);
btn[i]ctionListener(nl);
if (i%2==1){
(btn[(i+19)/2]);
btn[(i+19)/2]ctionListener(ol);
}
}
(centerPanel, ER);
Panel northPanel = new Panel();
ditable(false);
(tf);
(northPanel, H);
}
public void go() {
();
isible(true);
}
public static void main(String[] args) {
new MyCalculator()();
}
/**
*採用成員內部類方式,實現探聽器接口,方便訪問主類內類內部成員。
*此類負責數字按鈕Action事件監聽和處理
*/
class NumberListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if (!append) {
ext("");
append=true;
}
String s=ext();
s+=ctionCommand();
ext(s);
if (!btn[10]abled()){
for(int i=10;i<=14;i++) btn[i]nabled(true);
}
}
}
/**
* 成員內部類,負責操作符按鈕的事件處理
*/
class OperatorListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if (!append) return;
for(int i=10;i<=14;i++) btn[i]nabled(false);
String s=ext();
long num=eLong(s);//get the number of textfield
append=false; //set append
switch(operator){
case '+':result+=num;break;
case '-':result-=num;break;
case '*':result*=num;break;
case '/':{
if (num==0) result=0;
else result/=num;
break;
}
case '=':result=num;break;
}
ext(eOf(result));
//set the value of result to textfield
String op=ctionCommand();
operator=At(0); // set operator
}
}
}
方法2:
——————————————————————————————————————————————
package wanwa;
import .*;
public class calc {
public static void main(String[] args) {
Scanner input = new Scanner();
tln("*****************簡易計算器****************");
tln("*/t/t/t/t/t*");
tln("* 使用説明: 1.加法 2.減法 3.乘法 4.除法 *");
tln("*/t/t/t/t/t*");
tln("*****************************************");
for(int i=0;i<100;i++){
t("/n請選擇運算規則:");
int num = Int();
switch(num){
case 1:
tln("/n******你選擇了加法******/n");
t("請輸入第1個加數:");
int jiashu1 = Int();
t("請輸入第2個加數:");
int jiashu2 = Int();
tln("運算結果為:" + jiashu1 + " + " + jiashu1 + " = " + (jiashu1 + jiashu2));
break;
case 2:
tln("/n******你選擇了減法******/n");
t("請輸入被減數:");
int jianshu1 = Int();
t("請輸入減數:");
int jianshu2 = Int();
tln("運算結果為:" + jianshu1 + " - " + jianshu2 + " = " + (jianshu1 - jianshu2));
break;
case 3:
tln("/n******你選擇了乘法******/n");
t("請輸入第1個因數:");
int chengfa1 = Int();
t("請輸入第2個因數:");
int chengfa2 = Int();
tln("運算結果為:" + chengfa1 + " * " + chengfa2 + " = " + (chengfa1 * chengfa2));
break;
case 4:
tln("/n******你選擇了除法******/n");
t("請輸入被除數:");
double chufa1 = Int();
t("請輸入除數:");
double chufa2 = Int();
tln("運算結果為:" + chufa1 + " / " + chufa2 + " = " + (chufa1 / chufa2) + " 餘 " + (chufa1 % chufa2));
break;
default:
tln("/n你的選擇有錯,請重新選擇!");
break;
}
}
}
}
第三種寫法:
——————————————————————————————————————————————
package wanwa;
import .*;
import t.*;
import g.*;
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();
ayout(layout);
constraints = new GridBagConstraints();
start = true;
result = 0;
lastCommand = "=";
displayField = new JTextField(20);
orizontalAlignment(T);
x = 0;
y = 0;
width = 4;
height = 1;
= ;
htx = 100;
hty = 100;
onstraints(displayField, constraints);
(displayField);
ActionListener = new InsertAction();
ActionListener command = new CommandAction();
// addButton("Backspace", 0, 1, 2, 1, );
// addButton("CE", 2, 1, 1, 1, );
// addButton("C", 3, 1, 1, 1, );
addButton("7", 0, 2, 1, 1, );
addButton("8", 1, 2, 1, 1, );
addButton("9", 2, 2, 1, 1, );
addButton("/", 3, 2, 1, 1, command);
addButton("4", 0, 3, 1, 1, );