计算器:
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 }
原文地址:https://www.cnblogs.com/LJHAHA/p/10211075.html
时间: 2024-11-11 09:48:07