栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java猜四位数字游戏

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java猜四位数字游戏

java猜四位数字游戏

游戏运行时会随机生成4位数字,并且4个数字不重复,然后用户在控制台每次输入4个数字,输入时使用空格分隔,程序会告诉用户对了几个,其中位置对了有几个

程序运行结果示例
猜四位数密码游戏开始
5 3 2 1
1个数字正确且位置正确
5 2 3 1
1个数字正确且位置正确
5 8 4 0
4个数字正确,其中2个位置正确,2个位置不对
5 4 8 0
恭喜你,猜对了!

完整源码
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Application {

    public static void main(String[] args) {
        Random random = new Random();
        int[] answer = randomDiff4();
        System.out.println(Arrays.toString(answer));

        System.out.println("猜四位数密码游戏开始");
        Scanner scanner = new Scanner(System.in);
        while(true) {
            try {
                String line = scanner.nextLine();
                String[] inputs = line.split(" ");
                int[] inputInt = new int[answer.length];
                for(int i=0; i
                    inputInt[i] = Integer.parseInt(inputs[i]);
                }
                if(match(inputInt, answer)) {
                    break;
                }
            } catch (Exception e) {

            }
        }
    }

    
    public static int[] randomDiff4() {
        Random random = new Random();
        int[] answer = {-1, -1, -1, -1};
        int alreadRandom = 0;
        for(int i=0; i
            while(true) {
                int r = random.nextInt(10);
                boolean find = false;
                for(int j=0; j
                    if(answer[j] == r) {
                        find = true;
                        break;
                    }
                }
                if(!find) {
                    answer[alreadRandom++] = r;
                    break;
                }
            }

        }
        return answer;
    }

    public static boolean match(int[] input, int[] answer) {
        int size = answer.length;
        // 数字正确的个数
        int numCount = 0;
        // 位置正确的个数
        int positionCount = 0;
        for(int i=0; i
            for(int j=0; j
                if(answer[i] == input[j]) {
                    numCount++;
                    if(i==j) {
                        positionCount++;
                    }
                    break;
                }
            }
        }

        if(positionCount == answer.length) {
            System.out.println("恭喜你,猜对了!");
            return true;
        }

        if(numCount == 0) {
            System.out.println(numCount + "个数字正确");
            return false;
        }

        if(numCount == positionCount) {
            System.out.println(numCount + "个数字正确且位置正确");
            return false;
        }

        System.out.println(numCount + "个数字正确," + "其中" + positionCount + "个位置正确," + (numCount-positionCount) + "个位置不对");
        return false;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/987984.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号