java猜数字组合小游戏
2010-10-29 15:27
183 查看
后台系统代码Sys.java
前台显示操作,使用swing组建,GuessNum.java
运行效果:
import java.util.*; /* * 提供一些用于系统获取随机数、判断是否正确等操作的静态方法 */ public class Sys { static String note = ""; public static int[] getNums(){ Random r = new Random(); int[] ALLNUMS = {0,1,2,3,4,5,6,7,8,9}; int[] nums = new int[4]; int len = 10; int j,temp; for(int i=0;i<4;i++){ j = r.nextInt(len); nums[i] = ALLNUMS[j]; len--; temp = ALLNUMS[len]; ALLNUMS[len] = ALLNUMS[j]; ALLNUMS[j] = temp; } return nums; } public static boolean check(int[] s,int[] u){ int a=0,b=0; //a代表‘对’,b代表‘错’ String pre = ""; //显示错误信息的数据部分 for(int i = 0;i<s.length;i++){ if(s[i]==u[i]){ a++; }else{ b++; //显示错误信息的对错部分 } pre += u[i]; } if(a==s.length){ note = "恭喜你答对了,答案是:"+pre+"/n"; return true; }else{ note += pre + " " + a + "对" + b + "错" +"/n"; return false; } } public static void clear(){ note = ""; } }
前台显示操作,使用swing组建,GuessNum.java
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class GuessNum extends JFrame{ String shows = ""; String resultlist = ""; private static int[] sysnums; //获取随即4位不重复数,放置在数组中 JScrollPane jsp = new JScrollPane(); JTextField input = new JTextField(shows); JPanel main = new JPanel(); JPanel operator = new JPanel(); JTextArea resultshow = new JTextArea(resultlist); JButton zero = new JButton("0"); JButton check = new JButton("提交"); JButton reset = new JButton("重置"); JMenuBar jmb = new JMenuBar(); JMenu choices = new JMenu("选项"); JMenu help = new JMenu("HELP"); JMenuItem helpitem = new JMenuItem("游戏说明"); JMenuItem clearall = new JMenuItem("重新开始"); JCheckBoxMenuItem showoption = new JCheckBoxMenuItem("显示提示",true); JMenuItem exit = new JMenuItem("退出"); public static void main(String[] args){ sysnums = Sys.getNums(); new GuessNum().goFrame("猜数字"); } public void goFrame(String title){ this.setTitle(title); this.setBounds(400, 200, 250, 300); this.setBackground(Color.PINK); this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.getContentPane().setLayout(new BorderLayout(0,5)); exit.addActionListener(new MenuItemHandler(0)); clearall.addActionListener(new MenuItemHandler(1)); choices.add(clearall); showoption.addItemListener(new MenuDisp()); choices.add(showoption); choices.add(exit);jmb.add(choices); helpitem.addActionListener(new MenuItemHandler(2)); help.add(helpitem);jmb.add(help); this.setJMenuBar(jmb); main.setLayout(new GridLayout(2,1,4,4)); operator.setBackground(Color.PINK); operator.setLayout(new GridLayout(4,3,4,4)); for(int i=1;i<=9;i++){ JButton b = new JButton(i+""); b.addActionListener(new NumListener(i)); operator.add(b); } zero.addActionListener(new NumListener(0)); operator.add(zero); check.addActionListener(new NumListener(10)); //10代表提交按钮事件 operator.add(check); reset.addActionListener(new NumListener(12)); //12代表重置事件 operator.add(reset); main.add(operator); jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); jsp.getViewport().add(resultshow); main.add(jsp); input.addActionListener(new NumListener(11)); //11代表文本框事件 input.setHorizontalAlignment(JTextField.CENTER); this.getContentPane().add("North", input); this.getContentPane().add("Center", main); this.setVisible(true); } class NumListener implements ActionListener{ //Button事件监听类 private int n; public NumListener(int n){ this.n = n; } public void actionPerformed(ActionEvent e) { if(this.n>=0 && this.n<=9){ shows += this.n+" "; input.setText(shows); }else if(this.n == 10){ if(shows.length()==8){ try{ String userstring = shows.replaceAll("[//s]", ""); int[] usernums = new int[userstring.length()]; for(int i=0;i<userstring.length();i++){ usernums[i] = Integer.parseInt(userstring.charAt(i)+""); } if(Sys.check(sysnums, usernums)){ JOptionPane.showMessageDialog(null, Sys.note, "Congratulation!", JOptionPane.WARNING_MESSAGE); //提示正确 sysnums = Sys.getNums(); //重新开始 }else{ resultshow.setText(Sys.note); } }catch (Exception f){ JOptionPane.showMessageDialog(null, " 输入不正确...", "ERROR!", JOptionPane.WARNING_MESSAGE); } }else{ JOptionPane.showMessageDialog(null, " 输入不正确...", "ERROR!", JOptionPane.WARNING_MESSAGE); } shows = ""; input.setText(""); }else if(this.n==11){ try{ shows = input.getText(); String userstring = shows.replaceAll("[//s]", ""); if(userstring.length()==4){ int[] usernums = new int[userstring.length()]; for(int i=0;i<userstring.length();i++){ usernums[i] = Integer.parseInt(userstring.charAt(i)+""); } if(Sys.check(sysnums, usernums)){ JOptionPane.showMessageDialog(null, Sys.note, "Congratulation!", JOptionPane.WARNING_MESSAGE); sysnums = Sys.getNums(); }else{ resultshow.setText(Sys.note); } }else{ JOptionPane.showMessageDialog(null, " 输入不正确...", "ERROR!", JOptionPane.WARNING_MESSAGE); } }catch (Exception f){ JOptionPane.showMessageDialog(null, " 输入不正确...", "ERROR!", JOptionPane.WARNING_MESSAGE); }finally{ input.setText(""); } }else{ shows = ""; input.setText(""); } } } class MenuItemHandler implements ActionListener{ //菜单项的事件监听类 private int m; public MenuItemHandler(int m){ //0代表‘退出’,1代表‘重新开始’,2代表‘帮助’ this.m = m; } public void actionPerformed(ActionEvent e) { if(this.m==0){ System.exit(0); }else if(this.m==1){ sysnums = Sys.getNums(); Sys.clear(); shows=""; resultlist = ""; resultshow.setText(""); input.setText(""); }else{ JOptionPane.showMessageDialog(null, "系统随即给出4个0-9的数不重复数组合,/n" + "每次猜测数都会有提示,根据提示猜出正确答案.", "ERROR!", JOptionPane.WARNING_MESSAGE); } } } class MenuDisp implements ItemListener{ public void itemStateChanged(ItemEvent e) { if(showoption.getState()){ jsp.setVisible(true); } else{ jsp.setVisible(false); } } } }
运行效果:
相关文章推荐
- Java实现-数字组合1
- Java实现-数字组合2
- java生成随机数字和字母组合
- 20170727 JAVA练习题:输入一个三位数字,将其倒序组合输出
- java 生成随机数字+字母组合 和字母组合
- java生成随机数字和字母组合
- java 生成数字,字符,特殊字符组合方式代码
- Java版纯字母、纯数字、字母数字组合验证码的生成
- Java字母加数字组合比较大小
- java 递归 实现数字的排列组合
- Java字母加数字组合比较大小
- Java 小游戏(猜我心中所想数字--三次机会) while(true){}
- Java随机生成字母数字组合的验证码
- Java生成随机数字和字母组合10位数
- java生成随机数字和字母组合
- POJ-4004:数字组合(用位移方法解组合数问题,Java版)
- Java打印一组数字的全部排序组合
- java正则表达式,大写字母、小写字母、特殊字符、数字 两种两种以上组合匹配
- POJ-4004:数字组合(用位移方法解组合数问题,Java版)
- 用改过的“数字进制转换”枚举字符组合,这是简化的版本。(java,算法研究)