为了账号安全,请及时绑定邮箱和手机立即绑定

java----猜字符小游戏

标签:
Java

刚学了方法,做了个小项目,猜字符小游戏,这里和大家分享一下。
我们通过要达到的效果来分析:

            1首先命令行窗口提示用户进入猜字符游戏,并显示“猜吧”            2然后用户输入一个五位数的字符串(不能重复),接着系统进行判断            3如果字符串为"EXIT"则退出游戏            4如果没猜对则提示猜对的字符的个数和猜对位置的个数            5如果猜对则给出分数,并退出游戏            6分数的话是一个字符一百分,猜错一次扣十分。我们来分析:这个游戏需要用到哪些数据:    1.char[] ch 用来接收系统产生的随机字符    2.char[] input 用来接收用户输入的字符    3.int score 用来接收分数    4.int count 用来接收猜错次数    5.int[] result 用来存放猜对个数和猜对位置个数 定义方法:   1. public static char[] generate(){//用来生成随机字符数组                            char[] ch = new char[5];                            ...                            return ch;        }    2.public static int[] check(char[] ch,char[] input){//用来检查猜中几个和猜中位置情况                            int[] result = new int[ch.length];                            ...                            return result;    }    代码如下:   1. public static char[] generate(){                            char[] letter = new char[26];//用来存放A-Z的字符                            for(int i =0;i<letter.length;i++){                                        letter[i]=(char)(i+65);//将字母对应编码强制转换为字符并存放在数组中                            }                            int index = 0;                            boolean[] flag = new boolean[letter.length];//用来标记已经选过的字符                            char[] ch = new char[5];                            while(index<ch.length){                                             int i = (int)(Math.random()*letter.length);                                             if(flag[i]){//排除已经选过的字符                                                     contiune;                                             }                                             ch[index]=letter[i];                                             index++;                                             flag[i]=true;                            }                            return ch;        }        2.public static in[] check(char[] ch,char[] input){                                int[] result = new int[2];                                for(int i= 0;i<ch.length;i++){                                            for(int j=0;j<input.length;j++){                                                        if(ch[i]==input[j]){                                                                result[0]++;//猜中字符的个数                                                                if(i==j){                                                                        result[1]++;//猜中位置的个数                                                                        break;//如果位置判断相同则不用扫描后面的字符了                                                                }                                                        }                                            }                                }                                return result;             }             接下来开始写main函数             public static void main(String[] agrs){                                 char[] ch = generate();                                 Scanner sc =new Sacnner(System.in);                                 int count =0;                                 int score =0;                                 System.out.println("欢迎来到猜字符游戏");                                 while(true){                                                System.out.println("猜吧")                                                String str = sc.nextInt().touppercase();//把输入的字符串转换为大写                                                if(str.equal("EXIT")){                                                            System.out.println("退出");                                                            break;                                                }                                                char input = str.toCharArrays();//字符串转换为字符数组                                                int result = check(ch,input);                                                if(result[1]==ch.length){                                                        score = 100*ch.length-10*count;                                                        System.out.println("猜对了,得分:"+score)                                                        break;                                                }                                                else{                                                            System.out.println("猜对字符个数为"+result[0]+"猜对字符位置个数为"+result[1]);                                                            count++;                                                }                                 }             }             这个游戏还可以加些小功能,比如通过改变猜测的字符串的长度来改变游戏难度,或者通过分数来限制游戏猜测次数等等,这里就不多写了,大家可以尝试下。

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消