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ù)字正確而位置不對(duì)的數(shù)的個(gè)數(shù)。
如正確答案為 5234,而猜的人猜 5346,則是 1A2B,其中有一個(gè)5的位置對(duì)了,記為1A,而3和4這兩個(gè)數(shù)字對(duì)了,而位置沒對(duì),因此記為 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="\tGuess\tResult"; 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);//對(duì)菜單項(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, "請(qǐng)輸入一個(gè)四位數(shù) ! ! !"); tfd.setText(""); return ; } if(tfd.getText().length()!=4){//監(jiān)聽輸入的是否為四為數(shù)的數(shù) JOptionPane.showMessageDialog(this, "請(qǐng)輸入一個(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'){//猜對(duì),游戲結(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);//不能對(duì)文本框繼續(xù)編輯 } count++; } if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("exit")){ System.exit(0);//對(duì)按下菜單中的退出項(xiàng)做出應(yīng)答 } if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("new")){ string="\tGuess\tResult";//對(duì)按下菜單中的新游戲項(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ù)字正確而位置不對(duì)的數(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)專題,也分享給大家:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java實(shí)現(xiàn)的簡單猜數(shù)字游戲代碼
- Java編寫猜數(shù)字小游戲
- java版簡單的猜數(shù)字游戲?qū)嵗a
- java實(shí)現(xiàn)猜數(shù)字小游戲
- Java實(shí)現(xiàn)的猜數(shù)字游戲示例
- java實(shí)現(xiàn)猜數(shù)字小游戲(Swing版)
- java編寫猜數(shù)字游戲
- Java實(shí)現(xiàn)猜數(shù)字小游戲(有次數(shù)限制)
- 用java實(shí)現(xiàn)猜數(shù)字游戲
- 使用Java實(shí)現(xiàn)一個(gè)不同難度(高、中、低)的猜數(shù)字游戲
相關(guān)文章
自己動(dòng)手寫的mybatis分頁插件(極其簡單好用)
最近做了個(gè)項(xiàng)目,需要用到mybatis分頁功能,網(wǎng)上找了很多插件,都不太合適,于是就自己動(dòng)手寫了個(gè)mybatis分頁插件功能,非常不錯(cuò),代碼簡單易懂,需要的朋友參考下吧2016-11-11淺談Java中實(shí)現(xiàn)深拷貝的兩種方式—clone() & Serialized
這篇文章主要介紹了Java中實(shí)現(xiàn)深拷貝的兩種方式—clone() & Serialized,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03spring aop底層源碼執(zhí)行邏輯剖析(源碼解析)
這篇文章主要介紹了spring aop底層源碼執(zhí)行邏輯剖析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-08-08Java驗(yàn)證時(shí)間格式是否正確方法類項(xiàng)目實(shí)戰(zhàn)
在很多場(chǎng)景中我們需要驗(yàn)證時(shí)間日期的是否屬于正確的格式,驗(yàn)證時(shí)間是否符合常規(guī)的,本文就來介紹一下幾種方式,感興趣的可以了解一下2022-04-04SpringBoot實(shí)現(xiàn)發(fā)送郵件、發(fā)送微信公眾號(hào)推送功能
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)發(fā)送郵件、發(fā)送微信公眾號(hào)推送功能,這里對(duì)成員變量JavaMailSender使用了@Resource注解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03SpringBoot項(xiàng)目打成War布署在Tomcat的詳細(xì)步驟
這篇文章主要介紹了SpringBoot項(xiàng)目打成War布署在Tomcat,本文分步驟結(jié)合圖文實(shí)例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03解決@ConfigurationProperties注解的使用及亂碼問題
這篇文章主要介紹了解決@ConfigurationProperties注解的使用及亂碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10