Java實現簡單計算器、抽票程式
阿新 • • 發佈:2019-01-02
計算器:
1 import java.awt.BorderLayout; 2 import java.awt.Container; 3 import java.awt.Font; 4 import java.awt.GridLayout; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 8 import javax.swing.JButton; 9 import javax.swing.JFrame; 10 import javax.swing.JOptionPane;11 import javax.swing.JPanel; 12 import javax.swing.JTextField; 13 14 public class Calculator extends JFrame implements ActionListener { 15 // 屬性 16 JTextField txtResult; 17 String a = "", b = "", operator = ""; 18 boolean beginDigit = false, beginOperator = false,beginEquals=false; 19 //beginDigit記錄此次輸入前是否輸入數字,beginOpertator記錄此次輸入前是否輸入運算子,用於處理多次連續輸入運算子的情況(5+-3) 20 //beginEquals記錄此次輸入前是否進行過運算 21 public Calculator() { 22 setTitle("計算器"); 23 setSize(240, 270); 24 setResizable(false); 25 setLocationRelativeTo(null); 26 setDefaultCloseOperation(EXIT_ON_CLOSE);27 28 Container contentPane = this.getContentPane(); 29 contentPane.setLayout(new BorderLayout(1, 5)); 30 JPanel pnlNorth = new JPanel(); 31 JPanel pnlCenter = new JPanel(); 32 33 pnlNorth.setLayout(new BorderLayout()); 34 pnlCenter.setLayout(new GridLayout(4, 4, 3, 3)); 35 36 Font font = new Font("Times Roman", Font.BOLD, 20); 37 38 contentPane.add(BorderLayout.NORTH, pnlNorth); 39 contentPane.add(BorderLayout.CENTER, pnlCenter); 40 41 txtResult = new JTextField(); 42 txtResult.setFont(font); 43 txtResult.setEnabled(false); 44 JButton btnClear = new JButton("C"); 45 btnClear.setFont(font); 46 btnClear.addActionListener(this); 47 48 pnlNorth.add(BorderLayout.CENTER, txtResult); 49 pnlNorth.add(BorderLayout.EAST, btnClear); 50 51 String[] captions = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "/", "=", }; 52 for (int i = 0; i < captions.length; i++) { 53 JButton btn = new JButton(captions[i]); 54 btn.setFont(font); 55 pnlCenter.add(btn); 56 btn.addActionListener(this); 57 } 58 } 59 public static void main(String[] args) { 60 JFrame frame = new Calculator(); 61 frame.setVisible(true); 62 } 63 64 @Override 65 public void actionPerformed(ActionEvent event) { 66 String cmd = event.getActionCommand(); 67 if ("0123456789".contains(cmd)) { 68 if(beginEquals==true) {//如果之前已進行過運算,再次直接輸入數字就需要進行清空 69 txtResult.setText(""); 70 a="";b = "";operator = "";beginDigit=false; 71 } 72 if (beginDigit == true) { 73 txtResult.setText(txtResult.getText() + cmd); 74 } else { 75 txtResult.setText(cmd); 76 beginDigit = true; 77 beginEquals=false;//需要進行置換,不然無法進行上一個if語句 78 } 79 beginOperator=true; 80 } 81 if ("+-*/".contains(cmd)) { 82 beginEquals=false; 83 if (operator != ""&&beginOperator==true) { 84 if (txtResult.getText() != null) { 85 b = txtResult.getText(); 86 if ("+".equals(operator)) { 87 a = String.valueOf(Float.parseFloat(a) + Float.parseFloat(b)); 88 }if ("-".equals(operator)) { 89 a = String.valueOf(Float.parseFloat(a) - Float.parseFloat(b)); 90 }if ("*".equals(operator)) { 91 a = String.valueOf(Float.parseFloat(a) * Float.parseFloat(b)); 92 }if ("/".equals(operator)) { 93 a = String.valueOf(Float.parseFloat(a) / Float.parseFloat(b)); 94 } 95 operator = cmd;//再次記錄運算子 96 txtResult.setText(a); 97 } 98 } else { 99 a = txtResult.getText(); 100 operator = cmd; 101 } 102 beginDigit = false; 103 beginOperator=false; 104 } 105 if (".".equals(cmd)) { 106 if(txtResult.getText().contains(".")) { 107 JOptionPane.showMessageDialog(null, "不可輸入多個小數點!"); 108 a=a.substring(0,a.length()-1);//刪除多餘的小數點 109 txtResult.setText(a); 110 } 111 txtResult.setText(txtResult.getText() + cmd); 112 } 113 if ("C".equals(cmd)) { 114 txtResult.setText(""); 115 a = ""; 116 b = ""; 117 operator = ""; 118 beginDigit = false; 119 beginOperator=false; 120 } 121 if ("=".equals(cmd)) { 122 b=txtResult.getText(); 123 if ("+".equals(operator)) { 124 a = String.valueOf(Float.parseFloat(a) + Float.parseFloat(b)); 125 }if ("-".equals(operator)) { 126 a = String.valueOf(Float.parseFloat(a) - Float.parseFloat(b)); 127 }if ("*".equals(operator)) { 128 a = String.valueOf(Float.parseFloat(a) * Float.parseFloat(b)); 129 }if ("/".equals(operator)) { 130 a = String.valueOf(Float.parseFloat(a) / Float.parseFloat(b)); 131 } 132 txtResult.setText(a); 133 a=txtResult.getText(); 134 beginEquals=true; 135 beginDigit=false; 136 beginOperator=false; 137 } 138 } 139 }
抽獎:
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Random; 4 5 public class Lottery { 6 public static void main(String[] args) { 7 List<Integer> list=new ArrayList<>(); 8 int[] result=new int[6]; 9 for(int i=0;i<32;i++) { 10 list.add(i+1); 11 } 12 Random random=new Random();//記得得初始化一個Random物件 13 for(int i=0;i<result.length;i++) { 14 // 隨機生成一個下標,下標的範圍不超過list.size() 15 int index=random.nextInt(list.size()); 16 //將list中下標為index的數刪掉 17 result[i]=list.remove(index); 18 } 19 for(int i=0;i<result.length;i++) { 20 System.out.printf("%02d ",result[i]); 21 } 22 } 23 }