java實(shí)現(xiàn)猜數(shù)字游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)猜數(shù)字游戲的具體代碼,供大家參考,具體內(nèi)容如下
游戲規(guī)則:
通常由兩個(gè)人玩,一方出數(shù)字,一方猜。出數(shù)字的人要想好一個(gè)沒有重復(fù)數(shù)字的4位數(shù),不能讓猜的人知道。猜的人就可以開始猜。每猜一個(gè)數(shù)字,出數(shù)者就要根據(jù)這個(gè)數(shù)字給出幾A幾B,其中A前面的數(shù)字表示數(shù)字正確位置也正確的數(shù)的個(gè)數(shù),而B前的數(shù)字表示數(shù)字正確而位置不對的數(shù)的個(gè)數(shù)。
如正確答案為 5234,而猜的人猜 5346,則是 1A2B,其中有一個(gè)5的位置對了,記為1A,而3和4這兩個(gè)數(shù)字對了,而位置沒對,因此記為 2B,合起來就是 1A2B。
游戲截屏:
Run.java:
package xjj.java.GuessNumber2; public class Run { public static void main(String[] args) { JGuessGame g=new JGuessGame(); g.str=GuessNumb.getNumber();//得到隨機(jī)的四位數(shù) }}
GuessNumb.java:
package xjj.java.GuessNumber2; public class GuessNumb { public static String getNumber(){//隨機(jī)產(chǎn)生四位數(shù) char[] ch=new char[4]; for(int i=0;i<ch.length;i++){ ch[i]=(char) ((int)(Math.random()*10)+’0’); } //System.out.println(ch); String str=new String(ch); System.out.println(str); return str; }}
JGuessGame.java:
package xjj.java.GuessNumber2;import javax.swing.*; import java.awt.Button;import java.awt.Color;import java.awt.Dialog;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Frame;import java.awt.GridLayout;import java.awt.JobAttributes;import java.awt.Label;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseListener; public class JGuessGame extends JFrame implements ActionListener{ String string='tGuesstResult'; int count=1; String str; JTextField tfd; JTextArea tar; JButton btn; public JGuessGame(){ super('Guess Game !');//用JFrame類的構(gòu)造方法設(shè)置標(biāo)題 this.setDefaultCloseOperation(EXIT_ON_CLOSE);//設(shè)置叉關(guān)閉功能 this.setResizable(false);//控制框架能否改變大小 Dimension dim=this.getToolkit().getScreenSize();//獲取屏幕分辨率 this.setBounds(dim.width/3, dim.height/5, dim.width/3, 2*dim.height/3);//設(shè)置框架大小與位置 this.setBackground(Color.lightGray);//設(shè)置框架背景顏色 this.getContentPane().setBackground(Color.lightGray); this.getContentPane().setLayout(new FlowLayout());//設(shè)置布局類型 JPanel p=new JPanel();//添加面板 p.setBackground(Color.lightGray); p.add(new JLabel('Input : ')); btn=new JButton('確定');//設(shè)置按鈕 tfd=new JTextField(20);//設(shè)置編輯框 p.add(tfd);//向面板添加按鈕和編輯框 p.add(btn); this.getContentPane().add(p);//向框架添加面板 tar=new JTextArea(20,20);//添加文本域 tar.setBackground(Color.lightGray); this.getContentPane().add(tar); tar.setEditable(false);//設(shè)置文本域?yàn)椴豢删庉? btn.addActionListener(this);//監(jiān)聽按鈕 addMyMenu();//添加菜單 this.setVisible(true);//顯示框架 } private void addMyMenu() { // TODO JMenuBar menuBar =new JMenuBar();//新建菜單欄 this.setJMenuBar(menuBar);//添加菜單欄 String menuStrs[]={'Game','Help'}; JMenu[] menu =new JMenu[menuStrs.length];//新建菜單 for(int i=0;i<menuStrs.length;i++){ menu[i]=new JMenu(menuStrs[i]); menuBar.add(menu[i]); } JMenuItem menuItemView = new JMenuItem('玩法');//新建菜單項(xiàng) JMenuItem menuItemExit = new JMenuItem('退出'); JMenuItem menuItemNew = new JMenuItem('新游戲'); JMenuItem menuItemPase = new JMenuItem('暫停'); //JMenuItem menuItemBook = new JMenuItem('排行榜'); menu[0].add(menuItemNew) ; menu[0].add(menuItemPase) ; //menu[0].add(menuItemBook) ; menu[0].addSeparator(); menu[1].add(menuItemView); menuItemView.setActionCommand('View'); menuItemPase.setActionCommand('Pase'); menuItemNew.setActionCommand('New'); menuItemExit.setActionCommand('Exit'); menu[0].add(menuItemExit) ; menuItemView.addActionListener(this);//對菜單項(xiàng)進(jìn)行監(jiān)聽 menuItemPase.addActionListener(this); menuItemNew.addActionListener(this); menuItemExit.addActionListener(this); } public String getTextField(){ return tfd.getText(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==btn){ try {//監(jiān)聽輸入 里是否存儲(chǔ)不是數(shù)字的字符 int x = Integer.parseInt(tfd.getText()); } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(this, '請輸入一個(gè)四位數(shù) ! ! !'); tfd.setText(''); return ; } if(tfd.getText().length()!=4){//監(jiān)聽輸入的是否為四為數(shù)的數(shù) JOptionPane.showMessageDialog(this, '請輸入一個(gè)四位數(shù) ! ! !'); tfd.setText(''); return ; } String strresult=Result.getResult(tfd.getText(), str);//得到結(jié)果 string=string+'n'+count+'t'+tfd.getText()+'t'+strresult;//將結(jié)果處理,并輸出到文本域 tar.setText(string); tfd.setText(''); if(strresult.charAt(0)==’4’&&strresult.charAt(2)==’4’){//猜對,游戲結(jié)束 System.out.println('congratulation'); JOptionPane.showMessageDialog(this, 'congratulation ! 小JJ萬歲 !'); tfd.setEditable(false); } if(count==20){//步數(shù)耗盡,游戲結(jié)束 JOptionPane.showMessageDialog(this, 'Game Over ! You Fail !'); tfd.setEditable(false);//不能對文本框繼續(xù)編輯 } count++; } if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase('exit')){ System.exit(0);//對按下菜單中的退出項(xiàng)做出應(yīng)答 } if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase('new')){ string='tGuesstResult';//對按下菜單中的新游戲項(xiàng)做出應(yīng)答 tfd.setEditable(true); tar.setText(''); tfd.setText(''); count=1; this.str=GuessNumb.getNumber(); } if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase('pase')){ JOptionPane.showMessageDialog(this, '點(diǎn)擊‘確定’繼續(xù)游戲 !!!'); } if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase('view')){ JOptionPane.showMessageDialog(this, '1、輸入一個(gè)四位數(shù)n2、根據(jù)顯示的幾A幾B進(jìn)行下一次輸入(A前面數(shù)字表示位置正確的數(shù)的個(gè)數(shù),而B前面的數(shù)字表示數(shù)字正確而位置不對的數(shù)的個(gè)數(shù))n3、直到顯示4A4B時(shí),游戲結(jié)束。n4、20次內(nèi)沒得到正確結(jié)果,游戲也結(jié)束,你輸了!'); } } }
Result.java:
package xjj.java.GuessNumber2; public class Result { public static String getResult(String str1,String str2) {//將猜的與原答案進(jìn)行比較,得到提示 int a=0,b=0; for(int i=0;i<str1.length();i++){//位置相同且數(shù)相同的 數(shù)的個(gè)數(shù) if(str1.charAt(i)==str2.charAt(i)){ b++; } } for(int i=0;i<str1.length();i++){ for(int j=0;j<str2.length();j++){//數(shù)相同的數(shù)的個(gè)數(shù) if(str1.charAt(i)==str2.charAt(j)){ a++; break; } } } System.out.println(a+' '+b); return a+'A'+b+'B';//返回結(jié)果 }}
初次用java做圖形界面,還有很多不足的地方,容我慢慢改進(jìn)哈!
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,也分享給大家:
C++經(jīng)典小游戲匯總
python經(jīng)典小游戲匯總
python俄羅斯方塊游戲集合
JavaScript經(jīng)典游戲 玩不停
java經(jīng)典小游戲匯總
javascript經(jīng)典小游戲匯總
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳解盒子端CSS動(dòng)畫性能提升2. CSS hack用法案例詳解3. PHP字符串前后字符或空格刪除方法介紹4. 使用HttpClient消費(fèi)ASP.NET Web API服務(wù)案例5. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 刪除上傳文件(三)6. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除7. input submit、button和回車鍵提交數(shù)據(jù)詳解8. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式9. ASP常用日期格式化函數(shù) FormatDate()10. 詳解瀏覽器的緩存機(jī)制
