温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java如何实现猜数字游戏

发布时间:2020-07-27 09:57:45 来源:亿速云 阅读:243 作者:小猪 栏目:编程语言

这篇文章主要为大家展示了java如何实现猜数字游戏,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

游戏规则:

通常由两个人玩,一方出数字,一方猜。出数字的人要想好一个没有重复数字的4位数,不能让猜的人知道。猜的人就可以开始猜。每猜一个数字,出数者就要根据这个数字给出几A几B,其中A前面的数字表示数字正确位置也正确的数的个数,而B前的数字表示数字正确而位置不对的数的个数。

如正确答案为 5234,而猜的人猜 5346,则是 1A2B,其中有一个5的位置对了,记为1A,而3和4这两个数字对了,而位置没对,因此记为 2B,合起来就是 1A2B。

游戏截屏:

java如何实现猜数字游戏

java如何实现猜数字游戏

Run.java:

package xjj.java.GuessNumber2;
 
public class Run {
 public static void main(String[] args) {
 JGuessGame g=new JGuessGame();
 g.str=GuessNumb.getNumber();//得到随机的四位数
 }
}

GuessNumb.java:

package xjj.java.GuessNumber2;
 
public class GuessNumb {
 public static String getNumber(){//随机产生四位数 
 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类的构造方法设置标题
 this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置叉关闭功能
 this.setResizable(false);//控制框架能否改变大小
 Dimension dim=this.getToolkit().getScreenSize();//获取屏幕分辨率
 this.setBounds(dim.width/3, dim.height/5, dim.width/3, 2*dim.height/3);//设置框架大小与位置
 this.setBackground(Color.lightGray);//设置框架背景颜色
 this.getContentPane().setBackground(Color.lightGray);
 this.getContentPane().setLayout(new FlowLayout());//设置布局类型
 
 JPanel p=new JPanel();//添加面板
 p.setBackground(Color.lightGray);
 p.add(new JLabel("Input : "));
 btn=new JButton("确定");//设置按钮
 tfd=new JTextField(20);//设置编辑框
 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);//设置文本域为不可编辑
 
 btn.addActionListener(this);//监听按钮
 
 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("玩法");//新建菜单项
 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);//对菜单项进行监听
 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 {//监听输入 里是否存储不是数字的字符
 int x = Integer.parseInt(tfd.getText());
 } catch (NumberFormatException e1) {
 JOptionPane.showMessageDialog(this, "请输入一个四位数 ! ! !");
 tfd.setText("");
 return ;
 }
 if(tfd.getText().length()!=4){//监听输入的是否为四为数的数
 JOptionPane.showMessageDialog(this, "请输入一个四位数 ! ! !");
 tfd.setText("");
 return ;
 }
 String strresult=Result.getResult(tfd.getText(), str);//得到结果
 string=string+"\n"+count+"\t"+tfd.getText()+"\t"+strresult;//将结果处理,并输出到文本域
 tar.setText(string);
 tfd.setText("");
 if(strresult.charAt(0)=='4'&&strresult.charAt(2)=='4'){//猜对,游戏结束
 System.out.println("congratulation");
 JOptionPane.showMessageDialog(this, "congratulation ! 小JJ万岁 !");
 tfd.setEditable(false);
 }
 if(count==20){//步数耗尽,游戏结束
 JOptionPane.showMessageDialog(this, "Game Over ! You Fail !");
 tfd.setEditable(false);//不能对文本框继续编辑
 }
 count++;
 }
 if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("exit")){
 System.exit(0);//对按下菜单中的退出项做出应答
 }
 if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("new")){
 string="\tGuess\tResult";//对按下菜单中的新游戏项做出应答
 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, "点击‘确定'继续游戏 !!!");
 }
 if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("view")){
 JOptionPane.showMessageDialog(this, "1、输入一个四位数\n2、根据显示的几A几B进行下一次输入(A前面数字表示位置正确的数的个数,而B前面的数字表示数字正确而位置不对的数的个数)\n3、直到显示4A4B时,游戏结束。\n4、20次内没得到正确结果,游戏也结束,你输了!");
 }
 }
 
}

Result.java:

package xjj.java.GuessNumber2;
 
public class Result {
 public static String getResult(String str1,String str2) {//将猜的与原答案进行比较,得到提示
 int a=0,b=0;
 for(int i=0;i<str1.length();i++){//位置相同且数相同的 数的个数
 if(str1.charAt(i)==str2.charAt(i)){
 b++;
 }
 }
 for(int i=0;i<str1.length();i++){
 for(int j=0;j<str2.length();j++){//数相同的数的个数
 if(str1.charAt(i)==str2.charAt(j)){
  a++;
  break;
 }
 }
 }
 System.out.println(a+" "+b);
 return a+"A"+b+"B";//返回结果
 }
}

以上就是关于java如何实现猜数字游戏的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI