A. 用java編寫一個簡單的計算器(包括加減乘除)圖形界面
public class ItemEvent事件
{
public static void main(String args[])
{
WindowOperation win=new WindowOperation();
win.setBounds(100,100,390,360);
win.setTitle("簡單計算器");
}
}
import java.awt.event.*;
import javax.swing.*;
public class OperatorListener implements ItemListener
{
JComboBox choice;
ComputerListener workTogether;
public void setJComboBox(JComboBox box)
{
choice=box;
}
public void setWorkTogether(ComputerListener computer)
{
workTogether=computer;
}
public void itemStateChanged(ItemEvent e)
{
String fuhao=choice.getSelectedItem().toString();
workTogether.setFuhao(fuhao);
}
}
B. java加法計算器代碼
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
num1 = new javax.swing.JTextField();
num2 = new javax.swing.JTextField();
result = new javax.swing.JTextField();
addBtn = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Num1:");
jLabel2.setText("Num2:");
jLabel3.setText("Num3:");
addBtn.setText("Add");
addBtn.addActionListener(new jisuanAC());
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(53, 53, 53)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(addBtn)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(31, 31, 31)
.addComponent(num1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(result)
.addComponent(num2, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGap(168, 168, 168))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(num1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(num2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(61, 61, 61)
.addComponent(addBtn)
.addContainerGap(81, Short.MAX_VALUE))
);
jMenu1.setText("Operation");
jMenu3.setText("Add");
jMenu1.add(jMenu3);
jMenuBar1.add(jMenu1);
jMenu2.setText("Exit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private class jisuanAC implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(e.getSource()== addBtn)
{
int number1 = Integer.parseInt(num1.getText());
int number2 = Integer.parseInt(num2.getText());
int rs = number1 + number2;
result.setText(String.valueOf(rs));
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton addBtn;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField num1;
private javax.swing.JTextField num2;
private javax.swing.JTextField result;
}
這是只有一個加法的例子!希望幫到你
C. 求一個用JAVA編寫的計算器程序(1)實現簡單加、減、乘、除的運算。 (
public
MyCalculator()
{
f
=
new
JFrame("計算器ByMdou");
Container
contentPane
=
f.getContentPane();
/****************菜單的創建開始**************************************/
JMenuBar
mBar
=
new
JMenuBar();
mBar.setOpaque(true);
mEdit
=
new
JMenu("編輯(E)");
mEdit.setMnemonic(KeyEvent.VK_E);
mCopy
=
new
JMenuItem("復制(C)");
mEdit.add(mCopy);
mPaste
=
new
JMenuItem("粘貼(P)");
mEdit.add(mPaste);
mView
=
new
JMenu("查看(V)");
mView.setMnemonic(KeyEvent.VK_V);
mView.add(new
JMenuItem("標准型"));
mView.add(new
JMenuItem("科學型"));
mView.addSeparator();
mView.add(new
JMenuItem("查看分組"));
mHelp
=
new
JMenu("幫助(H)");
mHelp.setMnemonic(KeyEvent.VK_H);
mHelp.add(new
JMenuItem("幫助主題"));
mHelp.addSeparator();
mHelp.add(new
JMenuItem("關於計算器"));
mBar.add(mEdit);
mBar.add(mView);
mBar.add(mHelp);
f.setJMenuBar(mBar);
contentPane.setLayout(new
BorderLayout
());
JPanel
pTop
=
new
JPanel();
tResult
=
new
JTextField("0.",26);
tResult.setHorizontalAlignment(JTextField.RIGHT
);
tResult.setEditable(false);
pTop.add(tResult);
contentPane.add(pTop,BorderLayout.NORTH);
JPanel
pBottom
=
new
JPanel();
pBottom.setLayout(new
BorderLayout());
JPanel
pLeft
=
new
JPanel();
pLeft.setLayout(new
GridLayout(5,1,3,3));
bM
=
new
JButton("
");
bM.setEnabled(false);
pLeft.add(bM);
bOther
=
new
JButton("MC");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);
bOther
=
new
JButton("MR");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);
bOther
=
new
JButton("MS");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);
bOther
=
new
JButton("M+");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);
D. java:編寫一個計算器小程序,要求可以做加減乘除運算
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
private static final long serialVersionUID = 8199443193151152362L;
private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),
bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),
bto_6=new JButton("6"),bto_cheng=new JButton("*"),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),
bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton("."),bto_deng=new JButton("="),bto_jia=new JButton("+");
JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,
bto_0,bto_dian,bto_deng,bto_jia};
private JTextField text_double;// = new JTextField("0");
private String operator = "="; //當前運算的運算符
private boolean firstDigit = true; // 標志用戶按的是否是整個表達式的第一個數字,或者是運算符後的第一個數字
private double resultNum = 0.0; // 計算的中間結果
private boolean operateValidFlag = true; //判斷操作是否合法
public Calculator()
{
super("Calculator");
this.setBounds(300, 300, 300, 300);
this.setResizable(false);
this.setBackground(Color.orange);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());//設置布局
text_double=new JTextField("0",20);//設置文本區
text_double.setHorizontalAlignment(JTextField.RIGHT);//設置水平對齊方式未右對齊
this.getContentPane().add(text_double,BorderLayout.NORTH);//將文本區添加到Content北部
JPanel panel=new JPanel(new GridLayout(5,4));//在內容窗口添加一個網格布局
this.getContentPane().add(panel);//添加panel面板
for(int i=0;i<button.length;i++)//在面板上添加按鈕
panel.add(button[i]);
for(int i=0;i<button.length;i++)
button[i].addActionListener(this);//為按鈕注冊
text_double.setEditable(false);//文本框不可編輯
text_double.addActionListener(this);//
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)//
{
String c= e.getActionCommand();//返回與此動作相關的命令字元串。
System.out.println("##########command is "+c);
if(c.equals("C")){
handleC(); //用戶按了「C」鍵
}
else if (c.equals("CE")) // 用戶按了"CE"鍵
{
text_double.setText("0");
}
else if ("0123456789.".indexOf(c) >= 0) // 用戶按了數字鍵或者小數點鍵
{
handleNumber(c); // handlezero(zero);
} else //用戶按了運算符鍵
{
handleOperator(c);
}
}
private void handleC() // 初始化計算器的各種值
{
text_double.setText("0");
firstDigit = true;
operator = "=";
}
private void handleNumber(String button) {
if (firstDigit)//輸入的第一個數字
{
text_double.setText(button);
} else if ((button.equals(".")) && (text_double.getText().indexOf(".") < 0))//輸入的是小數點,並且之前沒有小數點,則將小數點附在結果文本框的後面
//如果字元串參數作為一個子字元串在此對象中出現,則返回第一個這種子字元串的第一個字元的索引;如果它不作為一個子字元串出現,則返回 -1
{
text_double.setText(text_double.getText() + ".");
} else if (!button.equals("."))// 如果輸入的不是小數點,則將數字附在結果文本框的後面
{
text_double.setText(text_double.getText() + button);
}
// 以後輸入的肯定不是第一個數字了
firstDigit = false;
}
private void handleOperator(String button) {
if (operator.equals("/")) {
// 除法運算
// 如果當前結果文本框中的值等於0
if (getNumberFromText() == 0.0){
// 操作不合法
operateValidFlag = false;
text_double.setText("除數不能為零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("+")){
// 加法運算
resultNum += getNumberFromText();
} else if (operator.equals("-")){
// 減法運算
resultNum -= getNumberFromText();
} else if (operator.equals("*")){
// 乘法運算
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
// 平方根運算
if(getNumberFromText()<0){
operateValidFlag = false;
text_double.setText("被開方數不能為負數");}
else
resultNum = Math.sqrt(resultNum);
}
else if (operator.equals("+/-")){
// 正數負數運算
resultNum = resultNum * (-1);
} else if (operator.equals("=")){
// 賦值運算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
// 雙精度浮點數的運算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
text_double.setText(String.valueOf(t1));
} else {
text_double.setText(String.valueOf(resultNum));
}
}
operator = button; //運算符等於用戶按的按鈕
firstDigit = true;
operateValidFlag = true;
}
private double getNumberFromText() //從結果的文本框獲取數字
{
double result = 0;
try {
result = Double.valueOf(text_double.getText()).doubleValue(); // ValueOf()返回表示指定的 double 值的 Double 實例
} catch (NumberFormatException e){
}
return result;
}
public static void main(final String[] args) {
new Calculator();
}
}
E. java編寫一個計算器類
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Jisuanqi extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Result result = new Result(); // 定義text的面板
Number_Key number_key = new Number_Key(); // 定義按鈕面板
// 當點擊按鈕+、-、*、/時,com = true
boolean com = false;
// 當i=0時說明是我們第一次輸入,字元串text不會累加
int i = 0;
// 存放text的內容
String text = "";
// 存放點擊按鈕+、-、*、/之前的數值
double defbutton = 0;
// +、-、*、/的代號分別為1,2,3,4
int symbol = 0;
// 構造函數
Jisuanqi() {
super("計算器"); // 設定標題
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 設定關閉窗體時退出程序
JPanel pane = new JPanel(); // 定義主面板
pane.setLayout(new BorderLayout());
setBounds(380, 220, 30, 80); // 前兩個參數是在屏幕上顯示的坐標,後兩個是大小
// 替換圖標
ImageIcon icon = new ImageIcon("F:1.GIF");
// Jisuanqi.class.getResource("APPLE.GIF")
// );
setIconImage(icon.getImage());
pane.add(result, BorderLayout.NORTH);
pane.add(number_key, BorderLayout.CENTER);
pane.add(number_key.equal, BorderLayout.SOUTH);
number_key.one.addActionListener(this); // 對1按鈕添加監聽事件
number_key.two.addActionListener(this); // 對2按鈕添加監聽事件
number_key.three.addActionListener(this); // 對3按鈕添加監聽事件
number_key.four.addActionListener(this); // 對4按鈕添加監聽事件
number_key.five.addActionListener(this); // 對5按鈕添加監聽事件
number_key.six.addActionListener(this); // 對6按鈕添加監聽事件
number_key.seven.addActionListener(this); // 對7按鈕添加監聽事件
number_key.eight.addActionListener(this); // 對8按鈕添加監聽事件
number_key.nine.addActionListener(this); // 對9按鈕添加監聽事件
number_key.zero.addActionListener(this); // 對0按鈕添加監聽事件
number_key.ce.addActionListener(this); // 對置零按鈕添加監聽事件
number_key.plus.addActionListener(this); // 對+按鈕添加監聽事件
number_key.equal.addActionListener(this); // 對=按鈕添加監聽事件
number_key.sub.addActionListener(this); // 對-按鈕添加監聽事件
number_key.mul.addActionListener(this); // 對*按鈕添加監聽事件
number_key.div.addActionListener(this); // 對/按鈕添加監聽事件
number_key.point.addActionListener(this); // 對.按鈕添加監聽事件
setContentPane(pane);
pack(); // 初始化窗體大小為正好盛放所有按鈕
}
// 各個按鈕觸發的事件
public void actionPerformed(ActionEvent e) {
/*
* 如果是點擊數字按鈕那麼先要判斷是否在此之前點擊了+、-、*、/、=,如果是那麼com=true 如果沒有com=
* false;或者是否點擊數字鍵,如果是i = 1,如果沒有 i = 0;
*/
if (e.getSource() == number_key.one) {
if (com || i == 0) {
result.text.setText("1");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "1");
}
} else if (e.getSource() == number_key.two) {
if (com || i == 0) {
result.text.setText("2");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "2");
}
} else if (e.getSource() == number_key.three) {
if (com || i == 0) {
result.text.setText("3");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "3");
}
} else if (e.getSource() == number_key.four) {
if (com || i == 0) {
result.text.setText("4");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "4");
}
} else if (e.getSource() == number_key.five) {
if (com || i == 0) {
result.text.setText("5");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "5");
}
} else if (e.getSource() == number_key.six) {
if (com || i == 0) {
result.text.setText("6");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "6");
}
} else if (e.getSource() == number_key.seven) {
if (com || i == 0) {
result.text.setText("7");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "7");
}
} else if (e.getSource() == number_key.eight) {
if (com || i == 0) {
result.text.setText("8");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "8");
}
} else if (e.getSource() == number_key.nine) {
if (com || i == 0) {
result.text.setText("9");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "9");
}
}
/*
* 對於0這個按鈕有一定的說法,在程序里不會出現如00000這樣的情況,加了判斷條件就是
* 如果text中的數值=0就要判斷在這個數值中是否有.存在?如果有那麼就在原來數值基礎之上添 加0;否則保持原來的數值不變
*/
else if (e.getSource() == number_key.zero) { // result.text.getText()是得到text里內容的意思
if (com || i == 0) {
result.text.setText("0");
com = false;
i = 1;
} else {
text = result.text.getText();
if (Float.parseFloat(text) > 0 || Float.parseFloat(text) < 0) { // Float.parseFloat(text)就是類型轉換了,下面都是一樣
result.text.setText(text + "0");
} else {
if (text.trim().indexOf(".") == -1) {
result.text.setText(text);
} else {
result.text.setText(text + "0");
}
}
}
} else if (e.getSource() == number_key.ce) {
result.text.setText("0");
i = 0;
com = true;
// text = "";
defbutton = 0;
}
/*
* 本程序不會讓一個數值中出現2個以上的小數點.具體做法是:判斷是否已經存在.存在就不添加, 不存在就添加.
*/
else if (e.getSource() == number_key.point) {
if (com || i == 0) {
result.text.setText("0.");
com = false;
i = 1;
} else {
text = result.text.getText();
if (text.trim().indexOf(".") == -1) {
result.text.setText(text + ".");
} else {
result.text.setText(text);
}
}
} // 獲得點擊+之前的數值
else if (e.getSource() == number_key.plus) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 1;
} // 獲得點擊-之前的數值
else if (e.getSource() == number_key.sub) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 2;
} // 獲得點擊*之前的數值
else if (e.getSource() == number_key.mul) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
System.out.println(defbutton);
symbol = 3;
} // 獲得點擊/之前的數值
else if (e.getSource() == number_key.div) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 4;
} else if (e.getSource() == number_key.equal) {
switch (symbol) {
case 1: { // 計算加法
double ad = defbutton
+ Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
case 2: { // 計算減法
double ad = defbutton
- Double.parseDouble(result.text.getText());
result.text.setText(String.valueOf(ad));
i = 0;
text = "";
break;
}
case 3: { // 計算乘法
double ad = defbutton
* Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
case 4: { // 計算除法
double ad = defbutton
/ Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
}
System.out.println(com);
}
System.out.println(result.text.getText());
}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Jisuanqi loveyou = new Jisuanqi();
loveyou.show();
}
}
// 計算器數字按鈕定義面板
class Number_Key extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton zero = new JButton("0"); // 數字鍵0
JButton one = new JButton("1"); // 數字鍵1
JButton two = new JButton("2"); // 數字鍵2
JButton three = new JButton("3"); // 數字鍵3
JButton four = new JButton("4"); // 數字鍵4
JButton five = new JButton("5"); // 數字鍵5
JButton six = new JButton("6"); // 數字鍵6
JButton seven = new JButton("7"); // 數字鍵7
JButton eight = new JButton("8"); // 數字鍵8
JButton nine = new JButton("9"); // 數字鍵9
JButton plus = new JButton("+");
JButton sub = new JButton("-");
JButton mul = new JButton("*");
JButton div = new JButton("/");
JButton equal = new JButton("=");
JButton ce = new JButton("清零"); // 置零鍵
JButton point = new JButton(".");
Number_Key() {
setLayout(new GridLayout(4, 4, 1, 1)); // 定義布局管理器為網格布局
setBackground(Color.blue); // 設置背景顏色
// 添加按鈕
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);
add(zero);
add(plus);
add(sub);
add(mul);
add(div);
add(point);
add(equal);
add(ce);
}
}
// 計算器顯示結果的窗體
class Result extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
// text先是輸入和結果
JTextField text = new JTextField("0");
@SuppressWarnings("deprecation")
Result() { // 講輸入的數字或得到的結果在text的右邊顯示
text.setHorizontalAlignment(SwingConstants.RIGHT);
text.enable(false); // 文本框不能編輯
setLayout(new BorderLayout()); // 設定布局管理器邊框布局
add(text, BorderLayout.CENTER); // text放置在窗體的中間
}
}
F. 用java編程加減乘除計算器
剛好用設計模式(簡單工廠模式)編寫了一個計算器。
packagecom.medavis.simplefactory.ui;
importjava.util.Scanner;
importcom.medavis.simplefactory.operate.OperatorFactory;
publicclassCaculatorUI{
privatestaticScannersc;
publicstaticvoidmain(String[]args){
try{
System.out.println("Pleaseinputanumber:");
sc=newScanner(System.in);
longfirst=sc.nextLong();
System.out.println("(+-*/):");
sc=newScanner(System.in);
Stringoperator=sc.nextLine();
System.out.println("Pleaseinputanumber:");
sc=newScanner(System.in);
longsecond=sc.nextLong();
OperatorFactoryopf=newOperatorFactory();
System.out.println(first+operator+second+"="+opf.getOperator(operator).getResult(first,second));
}catch(Exceptione){
System.out.println("Yourinputiswrong!");
}
}
}packagecom.medavis.simplefactory.operate;
{
@Override
publiclonggetResult(longfirst,longsecond){
returnfirst+second;
}
}
packagecom.medavis.simplefactory.operate;
{
@Override
publiclonggetResult(longfirst,longsecond){
returnfirst/second;
}
}
packagecom.medavis.simplefactory.operate;
publicclassOperatorFactory{
publicOperatorgetOperator(Stringoperator)throwsException{
switch(operator){
case"+":
returnnewOperatorAdd();
case"-":
returnnewOperatorSub();
case"*":
returnnewOperatorMul();
case"/":
returnnewOperatorDiv();
default:
thrownewException();
}
}
}
packagecom.medavis.simplefactory.operate;
{
@Override
publiclonggetResult(longfirst,longsecond){
returnfirst*second;
}
}
packagecom.medavis.simplefactory.operate;
{
@Override
publiclonggetResult(longfirst,longsecond){
returnfirst-second;
}
}
G. java 實現一個簡單的計算器,能做100以內的加減乘除
網上找的,如果你是要求的這樣的,自己寫也要好一會:
import java.awt.*;
import java.awt.event.*;
public class Calculator {
private Frame f;
private TextField tf = new TextField(30);
private long result;
private boolean append = false;
private Button[] btn = new Button[15];
private char operator = '=';
public static void main(String[] args) {
new Calculator().go();
}
public Calculator() {
initComponent();
}
private void initComponent() {
f = new Frame("Java Calculator");
f.setLayout(new BorderLayout()); // The frame uses BorderLayout
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
Panel centerPanel = new Panel();
centerPanel.setLayout(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(String.valueOf(i));
centerPanel.add(btn[i]);
btn[i].addActionListener(nl);
if (i % 2 == 1) {
centerPanel.add(btn[(i + 19) / 2]);
btn[(i + 19) / 2].addActionListener(ol);
}
}
f.add(centerPanel, BorderLayout.CENTER);
Panel northPanel = new Panel();
tf.setEditable(false);
northPanel.add(tf);
f.add(northPanel, BorderLayout.NORTH);
}
public void go() {
f.pack();
f.setVisible(true);
}
/**
* 採用成員內部類方式,實現監聽器介面,方便訪問主類內類內部成員。 此類負責數字按鈕Action事件監聽和處理
*/
class NumberListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (!append) {
tf.setText("");
append = true;
}
String s = tf.getText();
s += e.getActionCommand();
tf.setText(s);
if (!btn[10].isEnabled()) {
for (int i = 10; i <= 14; i++)
btn[i].setEnabled(true);
}
}
}
/**
* 成員內部類,負責操作符按鈕的事件處理
*/
class OperatorListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (!append)
return;
for (int i = 10; i <= 14; i++)
btn[i].setEnabled(false);
String s = tf.getText();
long num = Long.parseLong(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;
}
tf.setText(String.valueOf(result));
// set the value of result to textfield
String op = e.getActionCommand();
operator = op.charAt(0); // set operator
}
}
}
H. java加減乘除計算器界面編程
來源:Code Wizards HQ
智能觀 編譯
孩子想學編碼的話,有很多方法可以展開學習。可以讓他們學著構建視頻游戲、創建動畫、開發移動應用程序和搭建網站。不管孩子喜歡哪種形式,都有大量的編碼書供他們快速學起來!
但是,怎麼才能找到一本好的兒童編碼書呢?
沒經驗的孩子的編碼書,應該涵蓋基本內容。
翻翻適合初學者的書,看看裡面是否有加減乘除基本數學運算,仔細看看帶變數、if / then語句、循環和函數的章節。你不需要理解這些概念是怎麼工作的,只要看看書的標題里有沒有提這些,如果沒有,這本書不適合新手。
許多好的兒童編碼書只用一種語言。
好的兒童編碼書只側重一種語言,可以是Scratch、Javascript或Python。如果你不知道孩子應該學哪種語言,可以買兩三種不同語言的書。
好的兒童編碼書有很多照片和插圖。
對於那些第一次接觸代碼的孩子來說,視覺吸引力很重要。好的兒童編碼書每一頁上都有豐富多彩的插圖,充分吸引孩子的關注。
好的兒童編碼書提供有用的、可轉移的知識。
學一門特定的語言並不像理解基本的編碼概念那麼重要。寫代碼的人可以從任何語言開始學起,然後輕松地轉入其他語言。有些語言不太適合孩子,如C和C++,很多成年人都學不好。
因此,根據以上幾項挑選原則,我們推出一份教孩子編碼的書籍,涵蓋塊編程、編程基礎語言、Web開發、移動開發、游戲編程、機器人和物聯網等幾大類,並標出適合孩子學習的年齡段,可以說是史上最全的兒童編碼書籍匯總了!如果你正在苦惱怎麼給孩子選書,快收下這份開學禮物吧~
塊編程類
學齡前的兒童也可以學慣用塊編程語言編碼。塊語言是高度圖形化的,易於學習。Scratch是為孩子們設計的一種流行的塊語言。
1. Coding with Scratch Workbook
(用Scratch編碼)
作者:DK出版公司
適合年齡: 6-9歲
在短短40頁的篇幅里,讀者就能學會用Scratch做任何事,從解決簡單的數學問題到構建、分享游戲。每章都包含指導孩子們完成簡單編碼項目的分步說明。
2. Learn to Program with Scratch:A Visual Introction to Programming with Games, Art, Science, and Math
(學會用Scratch編程:游戲、藝術、科學和數學編程的可視化介紹)
作者:Majed Marji
適合年齡: 10歲及以上
這本將近300頁的書涵蓋了初學者需要了解的關於Scratch語言的所有內容。讀完這本書後,孩子們將會牢固掌握Scratch和適用於任何語言的編程思想。
3. Scratch for Kids For Dummies
(兒童Scratch傻瓜書)
作者:Derek Breen
適合年齡: 10-14歲
讀者將在構建16個項目中,學習變數、if / then語句、循環和基本編程邏輯。
4. The Official ScratchJr Book: Help Your Kids Learn to Code
(中文版:動手玩轉ScratchJr編程)
作者:Marina Umaschi Bers 、Mitchel Resnick
適合年齡: 5-9歲
對於那些還太小不能學習Scratch的孩子來說,Scratchr Jr是一種更簡單的塊編程語言。
5. MicroWorlds EX Books for Kids
(兒童MicroWorlds EX工具書)
作者:Steve Robson
適合年齡: 5-9歲
藉助MicroWorlds EX工具,孩子們通過圖形和文本來學習。他們沒有被一種純圖形語言和「固定」代碼所束縛,也不必100 %處理文本。
鏈接:
https://pan..com/s/1igfVJVV1UpBdIZvNzogv2Q
6. Coding for Kids For Dummies
(兒童編碼傻瓜書)
作者:Camille McCue
適合年齡: 10-14歲
書中指導孩子完成15個項目,包括繪畫、動畫和游戲,還帶領孩子們完成了創建計算機程序的過程,從構思到構建、測試。
編程基礎
盡管C++、Java和Python等語言之間存在著巨大的差異,但理解編碼基礎知識,孩子可以相對輕松地從一種語言遷移到另一種語言。
Python
7. Python for Kids
(適合孩子的Python學習)
作者:Jason R. Briggs
適合年齡: 10歲以上
這本書對孩子來說既全面又容易理解。內容適合小學生,但也適合許多成人學習者看。
鏈接:
https://pan..com/s/1GJUXzKUfVed3O-sfss3MDg
8. Coding Projects in Python
(Python編碼項目)
作者:DK出版公司
適合年齡: 9-12歲
不管孩子已經學了Scratch之類的塊語言,還是沒有做過任何編程,都可以在這本書里找到需要的一切來構建軟體。書後面的詞彙和參考部分對孩子來說很有幫助。
鏈接:
https://pan..com/s/1BJE0IgfeBc8uEHrdHAxuaw
9. Python in Easy Steps
(學習Python的簡單步驟)
作者:Mike McGrath
適合年齡:中學及以上
本書不是專門為孩子寫的,但它內容組織得很好,中學生,尤其是有一些Scratch或其他語言經驗的中學生,可以很容易地理解它。讀完本書,學生可以用Python構建簡單的網頁和編寫桌面應用程序。
10. Hello World! Computer Programming for Kids and Other Beginners
(中文版:父與子的編程之旅:與小卡特一起學Python)
作者:Warren Sande
適合年齡: 12歲以上
學習編碼時,大家編寫的第一個程序就是在屏幕上顯示「Hello World」。本書沿襲了這一傳統,並為新手提供構建任何類型有用軟體所需的必要技能。
鏈接:
https://pan..com/s/1tue23Fur36Q3l-8YoziJ1w
Java
Java是一種非常流行的語言,用於所有操作系統和Android設備。這也是高中計算機科學課程中最常見的語言。
11. Java Programming for Kids: Learn Java Step by Step and Build Your Own Interactive Calculator for Fun!
(面向兒童的Java編程:逐步學習Java,並構建自己的互動式計算器)
作者:R. Chandler Thompson
適合年齡: 13-17歲
這本書既有趣又簡單。青少年讀者可以通過Java編程語言,構建互動式計算器應用程序。
12. Java for Kids (and grown ups): Learn to Code and Create Your Own Projects with Java 8
(面向兒童和成年人的Java:學慣用Java 8編寫代碼並創建自己的項目)
作者:Nadia Ameziane Garcia
適合年齡: 10-12歲
這本書只介紹學習過程中每一步所需要的東西。每一章都建立在前文介紹的基礎上,孩子們可以在短時間內學會Java的基礎知識。
C++
這不是最容易學的語言,但精通C++的人會更了解計算機是如何工作的。
13. C++ Progamming In Easy Steps
(C++編程的簡單步驟)
作者:Mike McGrath
適合年齡:中學及以上
作者通過一系列簡單的課程介紹了C++,並提供大量專家提示,以幫助讀者獲得最佳編程實踐。這本書讓孩子們短時間內學會C++基礎知識,並可能沿用很多年。
14. C++ for Kids
(適合兒童的C++語言)
作者:Blaise Vanden-Heuvel、John C Vanden-Heuvel Sr.
適合年齡: 7-9歲
這本簡短多彩的書向孩子們展示了C++編程是如何工作的。示常式序各色各樣,這樣孩子們就能理解程序的各個部分實際上是做什麼的。
Web開發類
HTML和CSS
網頁用HTML構建,用CSS設計風格。這些不是真正的編程語言,但它們讓孩子接觸文本編輯器、構思想法,對日後探索Javascript或Python之類的語言很有用。
15. Build Your Own Website, a Comic Guide to HTML, CSS and WordPress
(建立你自己的網站,HTML、CSS和WordPress的漫畫指南)
作者:Nate Cooper、Kim Gee
適合年齡:中學及以上
在這本漫畫風格的網站搭建指南中,孩子們學習HTML標記語言,熟悉控制網頁樣式的CSS語言,了解內容管理系統WordPress。
鏈接:
https://pan..com/s/194OAJuxV47aUdHndpjzXhA
16. CoderDojo Nano: Building a Website: Create with Code
(用代碼搭建網站)
作者:Clyde Hatter、CoderDojo
適合年齡: 8-12歲
這本書只有96頁,引導孩子們完成建立網站的整個過程。孩子可以建立一個和書中例子完全一樣的網站,也可以試著建自己喜歡的網站。
17. Creating a Website: Design and Build Your First Site
(創建網站:設計和建立你的第一個網站)
作者:Greg Rickaby
適合年齡: 7-11歲
這本書為孩子設計網頁提供指南,從規劃網站的結構到添加使網站與眾不同的細節。同時提供了軟體工程師在現實世界中使用的許多技巧和操作過程。
JavaScript
許多程序員認為JavaScript是互聯網的未來,學習這種語言可以建立互動性強的、有用的網站。
18. Computer Coding with Javascript
(使用Javascript進行計算機編碼)
作者:DK出版公司
適合年齡: 8-12歲
從HTML、CSS轉到JavaScript可能容易暈,因為JavaScript更加復雜、強大。但這本書內容比較簡單,孩子們可以順利過渡。
19. Javascript for Kids: A Playful Introction to Programming
(面向兒童的JavaScript:有趣的編程入門)
作者:Nick Morgan
適合年齡: 10歲以上
本書從數組、變數類型討論到游戲編程。無論孩子想製作游戲、製作動畫還是使用虛擬現實,JavaScript都能讓他們把想法變成真實的程序。
鏈接:
https://pan..com/s/1JEs-oTMGwV2OgCcRdlfJEQ
20. Javascript for Kids For Dummies
(面向兒童的JavaScript傻瓜書)
作者:Chris Minnick、Eva Holland
適合年齡: 10-13歲
如果你有興趣讀編程書,幫孩子學習編碼,試試這本。這本書遠遠超出基本內容,方便讀者構建任何能想到的Web項目。
鏈接:
https://pan..com/s/1JfXJvj0xPSdGGw-Iwr_IJA
21. Get Coding! Learn HTML, CSS, Javascript & Build a Website, App & Game
(編碼!學習HTML,CSS,Javascript ;建立一個網站,應用程序和游戲)
作者:Young Rewired State
適合年齡: 9-12歲
這本書中的課程是以任務形式呈現的,教導孩子構建網站、應用程序和游戲。他們還會了解不同類型的編程語言以及每種語言的優點。
移動開發
安卓應用
App Inventor是一個完全在線開發的Android編程環境,具有類似Scratch的塊編程界面。它簡化了移動開發過程,是向孩子們介紹Android編程的理想方式。
22. Building a Mobile App
(構建移動應用程序)
作者:Guthals
適合年齡: 7-11歲
讀這本書的孩子不僅學習編碼,還學習規劃、開發自己的定製應用。本書提供了專業人員使用的應用程序開發的內容,包括原型製作和創建應用程序框架,對熱愛移動開發的孩子來說特別有價值。
23. Hello App Inventor
(你好,App Inventor)
作者:Paula Beer、Carl Simmons
適合年齡: 9歲以上
使用App Inventor構建的應用可以利用許多智能手機功能,如運動感測器、GPS和相機。本書通過30個示例項目向讀者展示了如何利用這些功能。
鏈接:
https://pan..com/s/1ybVo8tEVE5TE3T8F4wnaqw
iOS應用
為了構建蘋果產品應用程序,開發人員使用了一種叫Swift的語言。創建這種語言時,軟體工程師想讓沒經驗的人容易學習。對於從未編程的孩子來說,這是一個很好的選擇。
24. Coding iPhone Apps for Kids
(適合孩子的iPhone應用程序編碼)
作者:Gloria Winquist、Matt McCarthy
適合年齡: 10歲以上
本書介紹了Swift語言和編碼基礎,並讓孩子們構建兩個真正的應用程序:一個生日跟蹤器和一個滑板游戲。
25. Hello Swift
(你好,Swift)
作者: Tanmay Bakshi、Lynn Beighley
適合年齡: 9歲以上
Tanmay Bakshi可以說是我們的老朋友了,智能觀從去年開始就報道他的故事。他4歲編碼,9歲就發布第一個應用程序。通過他的書,讀者也會構建能在iOS應用商店上發布的應用程序。
如果還有人對他不了解,可以看看我們往期的報道。附傳送門:
13歲天才少年在IBM掀起一場人工智慧風暴
IBM少年工程師說:人工智慧的下一個前沿是心理健康
游戲編程
Scratch游戲編程
26. Coding Games in Scratch
(用Scratch編碼游戲)
作者:Jon Woodcock
適合年齡: 8-12歲
這本書讓孩子在瀏覽器中製作自己的游戲。
27. Code Your Own Games: 20 Games to Create with Scratch
(編碼自己的游戲: 用Scratch創建20個游戲)
作者: Max Wainewright
適合年齡: 6-11歲
這本書分5個層次遞進,20個游戲項目讓孩子們熟悉編碼和游戲開發的原理。每款游戲都鍛煉了孩子們的能力,比如控制玩家互動、在屏幕上設置對象動畫、添加聲音。
28. Star Wars Coding Projects
(星球大戰編碼項目)
作者:Jon Woodcock、Kiki Prottsman
適合年齡: 8-12歲
喜歡《星球大戰》的孩子可以通過C3PO、R2D2和其他角色學習編碼。讀者不需要任何編程經驗就可以看這本書。
Python游戲編程
29. Mission Python
(Python任務)
作者:Sean McManus
適合年齡: 10歲以上
雖然對於10歲的孩子來說,這本書有點簡單了,但它探索了Python游戲編程的深度。書從Python Essentials插件開始,逐漸深入更高級的概念,如音頻文件處理和3D圖形。
30. Invent Your Own Computer Games with Python
(中文版:Python編程快速上手——讓繁瑣工作自動化)
作者:Al Sweigart
適合年齡: 10歲以上
這本書通過對Hangman和Tic-Tac-Toe等幾款流行游戲進行詳細解釋來教授編程。
鏈接:
https://pan..com/s/1ncZTTrIPlBzA30e_BNjDAA
我的世界
31. Learn to Program with Minecraft
(用我的世界學項目)
作者:Craig Richardson
適合年齡: 10歲以上
《我的世界》是一款受歡迎的3D游戲,孩子們可以在虛擬世界中探索、建造城堡等。本書介紹了如何使用Python編程擴展《我的世界》。
鏈接:
https://pan..com/s/13_FMKoqlMF8P1uu4gaqRNQ
32. Minecraft Modding for Kids For Dummies
(用我的世界教孩子建模的傻瓜書)
作者:Guthals、Stephen Foster、 Lindsey Handley
適合年齡: 10-13歲
通過9個JavaScript項目,孩子們從《我的世界》里的「玩家」化身為 「建模師」。讀者要有玩《我的世界》的經驗,但不需要以前學過編碼。
機器人和物聯網
Arino
Arino是一台微型計算機,可以用感測器、LED燈和馬達等小硬體控制各種電路。學習Arino不僅僅意味著學習編碼;它包括設計和建造真實世界的項目。
33. Adventures in Arino
(Arino歷險記)
作者:Becky Stewart
適合年齡: 11-15歲
喜歡機械和工程的孩子會喜歡本書中的項目。孩子們了解Arino,然後上手一些簡單的項目,比如讓LED燈亮起來。
鏈接:
https://pan..com/s/1JFopfxxfONYCGAKMM28yaQ
34. Sylvia』s Super-Awesome Project Book: Super-Simple Arino
(Sylvia的超棒項目書:超級簡單的Arino)
作者:Sylvia "Super-Awesome" Todd
適合年齡: 8-12歲
Sylvia介紹了如何建立Arino項目來控制燈光和製作音樂等。這些項目不是復制代碼,羅列事物,而是對事物如何工作以及為什麼工作進行了詳細解釋。
樹莓派
你可以把樹莓派(Raspberry Pi)當成Arino的升級版。兩台計算機的尺寸差不多,但是樹莓派的操作系統更接近筆記本電腦。
35. Raspberry Pi Projects Workbook
(樹莓派項目工作簿)
作者:DK出版公司
適合年齡: 7-12歲
孩子們可要通過這本書製作游戲,製作音樂,製作動畫,學習Scratch、Python和Raspberry Pi。
鏈接:
https://pan..com/s/1AzZ-ZubG41zOXUh2IFRA
36. Adventures in Raspberry Pi
(樹莓派歷險記)
作者:Carrie Anne Philbin
適合年齡: 11-15歲
本書介紹的9個項目讓孩子們快速上手樹莓派,涵蓋的概念編程有基礎、游戲編程、圖形創建等。
鏈接:
https://pan..com/s/1Ub365kwYczlN0E5k6b-57g
樂高機器人
樂高生產了幾個套件,包括機器人滾動、旋轉甚至攀爬的所有必要部件。他們也有自己的語言,孩子們用這些語言來創作。
37. The Art of Lego Mindstorms EV3 Programming
(樂高頭腦風暴EV3編程的藝術)
作者:Terry Griffin
適合年齡: 10歲以上
本書教孩子們在EV3頭腦風暴編程環境中,控制自己的機器人。他們學習如何建造樂高機器人並用代碼控制它。
38. Building Robots with Lego Mindstorms
(用樂高頭腦風暴套件製造機器人)
作者: Mario Ferrari、Giulio Ferrari、Ralph Hempel
適合年齡: 10歲以上
在本書中,孩子們使用電子電路和感測器,以及樂高頭腦風暴套件,學習製造機器人。書中有大量關於齒輪、馬達、氣動和其他工程概念的信息,對於嚴肅的機器人愛好者來說很重要。
適合學齡前兒童的編碼書
為低齡兒童設計的編碼書,幫助兒童建立對計算機程序的模式、顏色和其他基本要素的認知。
39. Baby Loves Coding
(寶貝喜歡編碼)
作者:Ruth Spiro
適合年齡:學前及以上
因為是給很小的孩子看的,書中沒有提供任何明確的編碼指導。它介紹了解決問題的方法,以及優秀程序員需要有的想法。
40. Learn to code: Robot Train』s Surprise Birthday Party (Volume 1)
(學會編碼:機器人火車的驚喜生日聚會I)
作者:by Jen Chiou
適合年齡:學前及以上
孩子在本書中了解編碼的先決條件,如決策和邏輯。通過觸摸書上像按鈕的圖片,孩子們幫助一輛虛構的機器人火車向生日派對運送用品,從而了解編碼邏輯是如何工作的。
41. HTML for Babies
(適合嬰兒的超文本標記語言)
作者:Sterling Children's
適合年齡: 3-5歲
HTML是一種簡單的標記語言,不需要復雜的邏輯,3-5歲的孩子可以通過本書,學會識別與網路編程相關的符號和顏色。
42. JavaScript for Babies
(適合嬰兒的JavaScript語言)
作者:Sterling Children's
適合年齡: 3-5歲
這本書讓孩子們了解JavaScript的一些基本結構和數據類型。
有趣的青少年編碼書
讓青少年讀一本編程類的書可能很困難,所以以下書籍通過游戲開發和其他有趣的項目,幫助青少年學習。
43. Computer Programming for Teens
(適合青少年的電腦編程)
作者:Mary E. Farrell
適合年齡:青少年
沒有編碼經驗的孩子和那些不知道學什麼語言的孩子,是本書的目標受眾。書中有一些C++、Python和Web編程語言的例子,孩子們將會找到最適合自己的編碼項目。
44. Game Programming for Teens
(適合青少年的游戲編程)
作者:Maneesh Sethi
適合年齡:青少年
本書介紹了一種名為BlitzMax的游戲專用語言,它使孩子們能在所有平台( Windows、Mac、Linux )構建游戲。
鏈接:
https://pan..com/s/1pe0gc_MlDdIeD3jp5YNE1w
適合女孩的編碼書,STEM入門
女孩可以通過編程提前接觸STEM。構建應用程序、開發游戲、編程機器人——這些活動為女孩們進入STEM領域奠定了堅實的基礎。
45. Women Who Launched the Computer Age
(開創計算機時代的女性)
作者:Laurie Calkhoven
適合年齡: 6-8歲
本書講述了6位在二戰期間為美國軍方從事秘密項目的天才女性。
46. Grace Hopper: Queen of Computer Code
(格雷斯·霍普:計算機代碼女王)
作者:Laurie Wallmark
適合年齡: 5歲以上
格雷斯·霍普是一位有創造力的計算機科學家和數學家,也是一位授勛的海軍軍官和總統自由勛章獲得者。孩子們會在她精彩的生活和成就中找到鼓勵。
47. Girls Who Code: Learn to Code and Change the World
(學會編碼並改變世界)
作者:Reshma Saujani
適合年齡: 10歲以上
Reshma Saujani是女孩編碼組織「Girls Who Code」的創始人,她在本書中介紹了電腦和編碼,以及在科技領域工作的女性的軼事。
培養兒童編碼興趣的通用讀物
編碼的孩子受益於問題解決、數學和決策能力。一些優秀的兒童計算機科學通用讀物側重於培養孩子對編碼的興趣。
48. Lauren Ipsum: A Story About Computer Science and Other Improbable Things
(勞倫·益普森:關於計算機科學和其他不可思議的故事)
作者:Carlos Bueno
適合年齡: 10歲以上
一個年輕的女孩需要找到回家的路,但要做到這一點,必須解決一個個難題……讀者不知不覺中,接觸到了編程邏輯、解決問題的技巧和其他計算機科學思想。
49. Once Upon an Algorithm
(從前有個演算法)
作者:Martin Erwig
適合年齡:中學及以上
演算法是解決問題的一組指令或過程。你可能覺得孩子們不太想看這個。但作者Martin Erwig通過類比、童話和電影引用,使演算法變得有趣。
原文鏈接:
http://www.codewizardshq.com/coding-books-for-kids/
—完—
擴展閱讀:
適合孩子學習編碼的7款工具套件
干貨|| 如何在線學習編碼?看看這 11個優秀的編程資源網站
教孩子編碼就夠了嗎?不!學校該培養的是計算思維
每個人都應該學習編碼嗎?吳恩達這樣回答……
如果學生編程能力勝於老師,編碼課該怎麼教?
親愛的朋友:
經常有讀者咨詢,有沒有適合孩子的編碼書推薦?
我們之前推薦過一些,但苦於無中文版,很多讀者找不到購買方式。
今天的推薦,內容比較全面,我們可愛的小編找遍了資源,將能找到電子版都找到,一並送給你。
其餘沒有資源的,網上也可以買到。
如果本文對你有幫助,別忘了轉發給你身邊的朋友,讓它幫助更多人。
祝安!
PS:為了方便與讀者探討,特意注冊了一個專用號。如果你也是我們的鐵桿讀者,想探討交流,可以加微信:znglmym。
智能觀 靈米
2018-9-12 於北京中關村
想知道AI加教育領域有哪些最新研究成果?
想要AI領域更多的干貨?
想了解更多專家的「智能觀」?
請前往:www.智能觀.com。
想交流溝通,請加負責人微信:znglmym
聲明:
編譯文章旨在幫助讀者了解行業新思想、新觀點及新動態,為原作者觀點,不代表智能觀觀點。
轉載智能觀原創文章,請聯系
智能觀小艾(微信號:zng2017618)!
關於我們
我們關注AI+教育。致力於提供高附加值的知識,以幫助每一位老師和我們的讀者不斷學習並提高技能。
我們努力讓發表的每一篇文章都具有最佳質量,以滿足讀者的需求。
I. 如何使用Java對象語言編寫一個加減乘除計算器要有代碼
下面文件名要為:JiSuanQi.java
importjava.awt.*;
importjava.awt.event.*;
publicclassJiSuanQi
{
Strings="",s1=null,s2=null;
Framef=newFrame("計算器");
TextFieldtf=newTextField(30);
Panelp1=newPanel();
Panelp2=newPanel();
Panelp3=newPanel();
Buttonbt1=newButton("=");
Buttonbt2=newButton("刪除");
Button[]bt=newButton[16];
intid=0;
publicstaticvoidmain(String[]args)
{
newJiSuanQi().init();
}
publicvoidinit()
{
f.setBackground(newColor(85,247,253));
f.setLayout(newBorderLayout(4,4));
p2.setLayout(newGridLayout(4,4,4,4));
p3.setLayout(newBorderLayout(4,4));
f.setResizable(false);
f.add(p1,BorderLayout.NORTH);
f.add(p2);
p3.add(bt2,BorderLayout.NORTH);
p3.add(bt1);
p1.add(tf);
f.add(p3,BorderLayout.EAST);
String[]b={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","復位","/"};
for(inti=0;i<16;i++)
{
bt[i]=newButton(b[i]);
p2.add(bt[i]);
}
bt[0].setForeground(Color.blue);
bt[1].setForeground(Color.blue);
bt[2].setForeground(Color.blue);
bt[4].setForeground(Color.blue);
bt[5].setForeground(Color.blue);
bt[6].setForeground(Color.blue);
bt[8].setForeground(Color.blue);
bt[9].setForeground(Color.blue);
bt[10].setForeground(Color.blue);
bt[12].setForeground(Color.blue);
bt[13].setForeground(Color.blue);
bt[3].setForeground(Color.red);
bt[7].setForeground(Color.red);
bt[11].setForeground(Color.red);
bt[15].setForeground(Color.red);
bt[14].setForeground(Color.red);
bt1.setForeground(Color.red);
bt2.setForeground(Color.red);
f.pack();
f.setVisible(true);
f.addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
}
);
bt[0].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s+=1;
s2+=1;
tf.setText(s);
}
}
);
bt[1].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s+=2;
s2+=2;
tf.setText(s);
}
}
);
bt[2].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s+=3;
s2+=3;
tf.setText(s);
}
}
);
bt[4].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s+=4;
s2+=4;
tf.setText(s);
}
}
);
bt[5].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s+=5;
s2+=5;
tf.setText(s);
}
}
);
bt[6].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s+=6;
s2+=6;
tf.setText(s);
}
}
);
bt[8].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s+=7;
s2+=7;
tf.setText(s);
}
}
);
bt[9].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s+=8;
s2+=8;
tf.setText(s);
}
}
);
bt[10].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s+=9;
s2+=9;
tf.setText(s);
}
}
);
bt[12].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s+=0;
s2+=0;
tf.setText(s);
}
}
);
bt[13].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s+='.';
s2+='.';
tf.setText(s);
}
}
);
bt[3].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s1=s;
s+='+';
id=1;
s2="";
tf.setText(s);
}
}
);
bt[7].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s1=s;
s+='-';
id=2;
s2="";
tf.setText(s);
}
}
);
bt[11].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s1=s;
s+='*';
id=3;
s2="";
tf.setText(s);
}
}
);
bt[14].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s="";
s2="";
tf.setText(s);
}
}
);
bt[15].addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
s1=s;
s+='/';
id=4;
s2="";
tf.setText(s);
}
}
);
bt1.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
if(id<1);
else{
s+='=';
doublea=Double.parseDouble(s1);
doubleb=Double.parseDouble(s2);
doublec=0;
switch(id)
{
case1:c=a+b;break;
case2:c=a-b;break;
case3:c=a*b;break;
case4:c=a/b;break;
}
s+=c;
tf.setText(s);
}
s="";s1="";s2="";id=0;
}
}
);
bt2.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
char[]c1;
char[]c2=newchar[s.length()-1];
c1=s.toCharArray();
for(inti=0;i<s.length()-1;i++)
c2[i]=c1[i];
s=s.valueOf(c2);
if(id<1)
{
s1=s;
}
if(s2.length()>=1)
{
char[]c3;
char[]c4=newchar[s2.length()-1];
c3=s2.toCharArray();
for(inti=0;i<s2.length()-1;i++)
c4[i]=c3[i];
s2=s2.valueOf(c4);
}
tf.setText(s);
}
}
);
}
}
J. 用JAVA編寫一個計算器
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importjavax.swing.SwingConstants;
{
/**
*
*/
=1L;
Resultresult=newResult();//定義text的面板
Number_Keynumber_key=newNumber_Key();//定義按鈕面板
//當點擊按鈕+、-、*、/時,com=true
booleancom=false;
//當i=0時說明是我們第一次輸入,字元串text不會累加
inti=0;
//存放text的內容
Stringtext="";
//存放點擊按鈕+、-、*、/之前的數值
doubledefbutton=0;
//+、-、*、/的代號分別為1,2,3,4
intsymbol=0;
//構造函數
Jisuanqi(){
super("WangJiao");//設定標題
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定關閉窗體時退出程序
JPanelpane=newJPanel();//定義主面板
pane.setLayout(newBorderLayout());
setBounds(380,220,30,80);//前兩個參數是在屏幕上顯示的坐標,後兩個是大小
//替換圖標
ImageIconicon=newImageIcon("F:1.GIF");
//Jisuanqi.class.getResource("APPLE.GIF")
//);
setIconImage(icon.getImage());
pane.add(result,BorderLayout.NORTH);
pane.add(number_key,BorderLayout.CENTER);
pane.add(number_key.equal,BorderLayout.SOUTH);
number_key.one.addActionListener(this);//對1按鈕添加監聽事件
number_key.two.addActionListener(this);//對2按鈕添加監聽事件
number_key.three.addActionListener(this);//對3按鈕添加監聽事件
number_key.four.addActionListener(this);//對4按鈕添加監聽事件
number_key.five.addActionListener(this);//對5按鈕添加監聽事件
number_key.six.addActionListener(this);//對6按鈕添加監聽事件
number_key.seven.addActionListener(this);//對7按鈕添加監聽事件
number_key.eight.addActionListener(this);//對8按鈕添加監聽事件
number_key.nine.addActionListener(this);//對9按鈕添加監聽事件
number_key.zero.addActionListener(this);//對0按鈕添加監聽事件
number_key.ce.addActionListener(this);//對置零按鈕添加監聽事件
number_key.plus.addActionListener(this);//對+按鈕添加監聽事件
number_key.equal.addActionListener(this);//對=按鈕添加監聽事件
number_key.sub.addActionListener(this);//對-按鈕添加監聽事件
number_key.mul.addActionListener(this);//對*按鈕添加監聽事件
number_key.div.addActionListener(this);//對/按鈕添加監聽事件
number_key.point.addActionListener(this);//對.按鈕添加監聽事件
setContentPane(pane);
pack();//初始化窗體大小為正好盛放所有按鈕
}
//各個按鈕觸發的事件
publicvoidactionPerformed(ActionEvente){
/*
*如果是點擊數字按鈕那麼先要判斷是否在此之前點擊了+、-、*、/、=,如果是那麼com=true如果沒有com=
*false;或者是否點擊數字鍵,如果是i=1,如果沒有i=0;
*/
if(e.getSource()==number_key.one){
if(com||i==0){
result.text.setText("1");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"1");
}
}elseif(e.getSource()==number_key.two){
if(com||i==0){
result.text.setText("2");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"2");
}
}elseif(e.getSource()==number_key.three){
if(com||i==0){
result.text.setText("3");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"3");
}
}elseif(e.getSource()==number_key.four){
if(com||i==0){
result.text.setText("4");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"4");
}
}elseif(e.getSource()==number_key.five){
if(com||i==0){
result.text.setText("5");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"5");
}
}elseif(e.getSource()==number_key.six){
if(com||i==0){
result.text.setText("6");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"6");
}
}elseif(e.getSource()==number_key.seven){
if(com||i==0){
result.text.setText("7");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"7");
}
}elseif(e.getSource()==number_key.eight){
if(com||i==0){
result.text.setText("8");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"8");
}
}elseif(e.getSource()==number_key.nine){
if(com||i==0){
result.text.setText("9");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"9");
}
}
/*
*對於0這個按鈕有一定的說法,在我的程序里不會出現如00000這樣的情況,我加了判斷條件就是
*如果text中的數值=0就要判斷在這個數值中是否有.存在?如果有那麼就在原來數值基礎之上添加0;否則保持原來的數值不變
*/
elseif(e.getSource()==number_key.zero){//result.text.getText()是得到text里內容的意思
if(com||i==0){
result.text.setText("0");
com=false;
i=1;
}else{
text=result.text.getText();
if(Float.parseFloat(text)>0||Float.parseFloat(text)<0){//Float.parseFloat(text)就是類型轉換了,下面都是一樣
result.text.setText(text+"0");
}else{
if(text.trim().indexOf(".")==-1){
result.text.setText(text);
}else{
result.text.setText(text+"0");
}
}
}
}elseif(e.getSource()==number_key.ce){
result.text.setText("0");
i=0;
com=true;
//text="";
defbutton=0;
}
/*
*本程序不會讓一個數值中出現2個以上的小數點.具體做法是:判斷是否已經存在.存在就不添加,不存在就添加.
*/
elseif(e.getSource()==number_key.point){
if(com||i==0){
result.text.setText("0.");
com=false;
i=1;
}else{
text=result.text.getText();
if(text.trim().indexOf(".")==-1){
result.text.setText(text+".");
}else{
result.text.setText(text);
}
}
}//獲得點擊+之前的數值
elseif(e.getSource()==number_key.plus){
com=true;
i=0;
defbutton=Double.parseDouble(result.text.getText());
symbol=1;
}//獲得點擊-之前的數值
elseif(e.getSource()==number_key.sub){
com=true;
i=0;
defbutton=Double.parseDouble(result.text.getText());
symbol=2;
}//獲得點擊*之前的數值
elseif(e.getSource()==number_key.mul){
com=true;
i=0;
defbutton=Double.parseDouble(result.text.getText());
System.out.println(defbutton);
symbol=3;
}//獲得點擊/之前的數值
elseif(e.getSource()==number_key.div){
com=true;
i=0;
defbutton=Double.parseDouble(result.text.getText());
symbol=4;
}elseif(e.getSource()==number_key.equal){
switch(symbol){
case1:{//計算加法
doublead=defbutton
+Double.parseDouble(result.text.getText());
result.text.setText(ad+"");
i=0;
text="";
break;
}
case2:{//計算減法
doublead=defbutton
-Double.parseDouble(result.text.getText());
result.text.setText(String.valueOf(ad));
i=0;
text="";
break;
}
case3:{//計算乘法
doublead=defbutton
*Double.parseDouble(result.text.getText());
result.text.setText(ad+"");
i=0;
text="";
break;
}
case4:{//計算除法
doublead=defbutton
/Double.parseDouble(result.text.getText());
result.text.setText(ad+"");
i=0;
text="";
break;
}
}
System.out.println(com);
}
System.out.println(result.text.getText());
}
@SuppressWarnings("deprecation")
publicstaticvoidmain(String[]args){
Jisuanqiloveyou=newJisuanqi();
loveyou.show();
}
}
//計算器數字按鈕定義面板
classNumber_KeyextendsJPanel{
/**
*
*/
=1L;
JButtonzero=newJButton("0");//數字鍵0
JButtonone=newJButton("1");//數字鍵1
JButtontwo=newJButton("2");//數字鍵2
JButtonthree=newJButton("3");//數字鍵3
JButtonfour=newJButton("4");//數字鍵4
JButtonfive=newJButton("5");//數字鍵5
JButtonsix=newJButton("6");//數字鍵6
JButtonseven=newJButton("7");//數字鍵7
JButtoneight=newJButton("8");//數字鍵8
JButtonnine=newJButton("9");//數字鍵9
JButtonplus=newJButton("+");
JButtonsub=newJButton("-");
JButtonmul=newJButton("*");
JButtondiv=newJButton("/");
JButtonequal=newJButton("=");
JButtonce=newJButton("清零");//置零鍵
JButtonpoint=newJButton(".");
Number_Key(){
setLayout(newGridLayout(4,4,1,1));//定義布局管理器為網格布局
setBackground(Color.blue);//設置背景顏色
//添加按鈕
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);
add(zero);
add(plus);
add(sub);
add(mul);
add(div);
add(point);
add(equal);
add(ce);
}
}
//計算器顯示結果的窗體
classResultextendsJPanel{
/**
*
*/
=1L;
//text先是輸入和結果
JTextFieldtext=newJTextField("0");
@SuppressWarnings("deprecation")
Result(){//講輸入的數字或得到的結果在text的右邊顯示
text.setHorizontalAlignment(SwingConstants.RIGHT);
text.enable(false);//文本框不能編輯
setLayout(newBorderLayout());//設定布局管理器邊框布局
add(text,BorderLayout.CENTER);//text放置在窗體的中間
}
}
直接復制保存成Jisuanqi.java可以直接運行了