Java編寫盤算器的罕見辦法實例總結。本站提示廣大學習愛好者:(Java編寫盤算器的罕見辦法實例總結)文章只能為提供參考,不一定能成為您想要的結果。以下是Java編寫盤算器的罕見辦法實例總結正文
本文實例總結了Java編寫盤算器的罕見辦法。分享給年夜家供年夜家參考,詳細以下:
辦法一:
package wanwa; 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); addButton("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); } }
辦法二:
import java.awt.*; import java.awt.event.*; 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"); 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); } public static void main(String[] args) { new MyCalculator().go(); } /** *采取成員外部類方法,完成監聽器接口,便利拜訪主類內類外部成員。 *此類擔任數字按鈕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 } } }
辦法三:
package wanwa; import java.util.*; public class calc { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("*****************簡略單純盤算器****************"); System.out.println("*\t\t\t\t\t*"); System.out.println("* 應用解釋: 1.加法 2.減法 3.乘法 4.除法 *"); System.out.println("*\t\t\t\t\t*"); System.out.println("*****************************************"); for(int i=0;i<100;i++){ System.out.print("\n請選擇運算規矩:"); int num = input.nextInt(); switch(num){ case 1: System.out.println("\n******你選擇了加法******\n"); System.out.print("請輸出第1個加數:"); int jiashu1 = input.nextInt(); System.out.print("請輸出第2個加數:"); int jiashu2 = input.nextInt(); System.out.println("運算成果為:" + jiashu1 + " + " + jiashu1 + " = " + (jiashu1 + jiashu2)); break; case 2: System.out.println("\n******你選擇了減法******\n"); System.out.print("請輸出被減數:"); int jianshu1 = input.nextInt(); System.out.print("請輸出減數:"); int jianshu2 = input.nextInt(); System.out.println("運算成果為:" + jianshu1 + " - " + jianshu2 + " = " + (jianshu1 - jianshu2)); break; case 3: System.out.println("\n******你選擇了乘法******\n"); System.out.print("請輸出第1個因數:"); int chengfa1 = input.nextInt(); System.out.print("請輸出第2個因數:"); int chengfa2 = input.nextInt(); System.out.println("運算成果為:" + chengfa1 + " * " + chengfa2 + " = " + (chengfa1 * chengfa2)); break; case 4: System.out.println("\n******你選擇了除法******\n"); System.out.print("請輸出被除數:"); double chufa1 = input.nextInt(); System.out.print("請輸出除數:"); double chufa2 = input.nextInt(); System.out.println("運算成果為:" + chufa1 + " / " + chufa2 + " = " + (chufa1 / chufa2) + " 余 " + (chufa1 % chufa2)); break; default: System.out.println("\n你的選擇有錯,請從新選擇!"); break; } } } }
辦法四:
package wanwa; 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); addButton("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法式設計有所贊助。