猜数字小游戏(JAVA)

题目描述

猜数字(又称 Bulls and Cows )是一种古老的的密码破译类益智类小游戏,起源于20世纪中期,一般由两个人或多人玩,也可以由一个人和电脑玩。通常由两个人玩,一方出数字,一方猜。出数字的人要想好一个没有重复数字4个数,不能让猜的人知道。猜的人就可以开始猜。每猜一个数字,出数者就要根据这个数字给出nAmB,其中A前面的数字n表示数字正确且位置也正确的数的个数,而B前的数字m表示数字正确但位置不正确的数的个数。如正确答案为 5234,而猜的人猜 5346,则是 1A2B,其中有一个5的位置对了,记为1A,而3和4这两个数字对了,而位置没对,因此记为 2B,合起来就是 1A2B。接着猜的人再根据出题者的几A几B继续猜,直到猜中(即 4A0B)为止。
程序要求:1、满足题意 2、输入数字的合法性3、输出总猜测次数

代码

package Practice;
// 猜数字 (Bulls and cows)
import java.util.Scanner;

public class Day0322 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 产生0000~9999的随机数
        double r = Math.random();
        int res = (int)(r * 8999 + 1000);

        int flag = 0;
        // 合法性检查,判断存在重复数字
        while(flag == 0)
        {
            int[] check = new int[10];
            for(int i = 0; i < 10; i ++ ) check[i] = 0;

            check[res / 1000] += 1;check[(res / 100) % 10] += 1;
            check[(res / 10) % 10] += 1;check[res % 10] += 1;

            for(int i = 0; i < 10; i ++ )
                if(check[i] >= 2) {
                    r = Math.random();
                    res = (int)(r * 8999 + 1000);
                    flag = 0;
                    break;
                }
                else flag = 1;
        }

        // 0000~9999
        System.out.println("答案: " + res);

        int input = -1;
        int idx = 0;
        int times = 0;
        while(input != res)
        {
            System.out.print("请输入你猜的数字: ");
            input = scanner.nextInt();
            int inputcopy = input;

            if(input < 0)
            {
                System.out.println("您输入的数字不是四位数!");
                times ++;
                continue;
            }
            int t = 0;
            // 输入数字为4位数,合法性检查
            while(inputcopy != 0)
            {
                inputcopy /= 10;
                t ++;
            }
            if(t != 4)
            {
                System.out.println("您输入的数字不是四位数!");
                times ++;
                continue;
            }

            int n = 0, m = 0;// nAmB

            if(input == res) break;
            // 输入的各个位数
            int[] a = new int[4];
            a[0] = input / 1000;a[1] = (input / 100) % 10;
            a[2] = (input / 10) % 10; a[3] = (input) % 10;
            // 答案的各个位数
            int[] ans = new int[4];
            ans[0] = res / 1000;ans[1] = (res / 100) % 10;
            ans[2] = (res / 10) % 10; ans[3] = (res) % 10;

            for(int i = 0; i < 4; i ++)
            {
                if(ans[i] == a[i]) n += 1; // A的数量
                for(int j = 0; j < 4; j ++){// B的数量
                    if(ans[j] == a[i] && j != i) m += 1;
                }
            }
            System.out.print((++ idx) + ": " + n + "A" + m + "B");
            System.out.println();
            times ++;

        }
        if(input == res){
            times ++;
            System.out.println("4A0B");
            System.out.println("你很厉害啊!");
            System.out.println("猜测次数: " + times);
        }

    }

}

运行效果

在这里插入图片描述

新增功能

在这里插入图片描述

思路

nAmB中,A的数量可直接遍历字符串的每个位置
input.charAt(i) == target.charAt(i)n ++
否则,分别对两个字符串计数出现的数字数量,需要分别开一个长度为10的数组。
由于是匹配问题,同一个数字在两个字符串中若出现次数不同,则只能取最小。累加即得m

代码

// 猜数字 (Bulls and cows)
import java.util.Scanner;

public class Main {
    // 判断是否为纯数字
    public  static boolean check(String str){
        for(int i = 0; i < str.length(); i ++ ){
            if(str.charAt(i) >= '0' && str.charAt(i) <= '9') continue;
            else return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 产生0000~9999的随机数
        double r = Math.random();
        int res = (int)(r * 9999);

        String target = String.format("%04d", res);

        // 0000~9999
        System.out.println("答案: " + target);

        String input = "";
        int idx = 0;
        int times = 0;

        while(!input.equals(target))
        {
            System.out.print("请输入你猜的4位数字: ");
            input = scanner.nextLine();
            if(input.length() != 4) {
                System.out.println("您输入的数字长度不是四位~");
                continue;
            }
            if(!check(input)) {
                System.out.println("您输入了非数字~");
                continue;
            }

            int n = 0, m = 0;// nAmB

            if(input.equals(target)) break;
            // 输入错误时计算n, m
            int[] cntN = new int[10];
            int[] cntM = new int[10];
            for(int i = 0; i < target.length(); i ++ ){
                if(input.charAt(i) == target.charAt(i)) n ++ ; // 数字对且位置对
                else{
                    cntN[target.charAt(i) - '0'] ++ ;
                    cntM[input.charAt(i) - '0'] ++ ;
                }
            }
            for(int i = 0; i < 10; i ++ ) m += Math.min(cntN[i], cntM[i]);

            System.out.print((++ idx) + ": " + n + "A" + m + "B");
            System.out.println();
        }

        if(input.equals(target)){
            idx ++;
            System.out.println("4A0B");
            System.out.println("你很厉害啊!");
            System.out.println("猜测次数: " + idx);
        }

    }

}

运行效果

在这里插入图片描述

  • 11
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值