java实现猜数游戏
游戏很简单就是100以内的一个数,先确定一个当做答案,然后挨个猜,然后给出答案范围,范围不断缩小,就可以猜出答案,以前在酒桌上经常玩这游戏。囧!!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class fhgj extends NewJFrame { int answer=(int)(Math.random()*100); public static void main(String[] args){ //NewJFrame j=new NewJFrame(); //j.show(); //System.out.print(j.jTextField1.getText()); NewJ games=new NewJ(); games.show(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class NewJ extends javax.swing.JFrame { fhgj jj=new fhgj(); private JLabel jLabel1; private JLabel jLabel2; private JTextField ans2; private JTextField ans1; private JButton sure; public JTextField ans; /** * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { NewJ inst = new NewJ(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); } public NewJ() { super(); initGUI(); } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); getContentPane().setLayout(null); this.setTitle("\u731c\u6570\u6e38\u620f"); { jLabel1 = new JLabel(); getContentPane().add(jLabel1); jLabel1.setText("\u731c100\u4e4b\u95f4\u7684\u6574\u6570"); jLabel1.setBounds(124, 33, 246, 40); jLabel1.setFont(new java.awt.Font("微软雅黑",0,20)); } { ans = new JTextField(); getContentPane().add(ans); ans.setText("\u7b54\u6848\u533a"); ans.setBounds(79, 72, 131, 50); } { sure = new JButton(); getContentPane().add(sure); sure.setText("\u786e\u5b9a"); sure.setBounds(246, 71, 92, 47); sure.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) {// String c=ans.getText(); int ac=Integer.parseInt(c); String a=ans1.getText(); String b=ans2.getText(); int lowans=Integer.parseInt(a); int upans=Integer.parseInt(b); if(jj.answer==ac) {JOptionPane.showMessageDialog(null,"恭喜你答对了"); }else {JOptionPane.showMessageDialog(null,"答案错了哦"); if(ac>jj.answer) {String ss=""+ac; ans2.setText(ss);}else {String ss=""+ac; ans1.setText(ss);} } } }); } { jLabel2 = new JLabel(); getContentPane().add(jLabel2); jLabel2.setText("\u6570\u636e\u8303\u56f4"); jLabel2.setBounds(151, 148, 115, 29); jLabel2.setFont(new java.awt.Font("微软雅黑",0,20)); } { ans1 = new JTextField(); getContentPane().add(ans1); ans1.setText("0"); ans1.setBounds(79, 213, 90, 51); } { ans2 = new JTextField(); getContentPane().add(ans2); ans2.setText("100"); ans2.setBounds(223, 211, 102, 54); } pack(); this.setSize(418, 333); } catch (Exception e) { //add your error handling code here e.printStackTrace(); } } } |
对java的机制理解还是很有限,一天一个小游戏,慢慢来吧!!!