用Java玩猜数字游戏

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String random2;
        int count = 1;
        int numLength;
        int keyLength;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please, enter the secret code's length:");

        try {
            numLength = scanner.nextInt();
            if (numLength <= 0) {
                System.out.println("Error: \" \" isn't a valid number.");
                return;
            }
        } catch (InputMismatchException e) {
            System.out.print("Error: \" \" isn't a valid number.");
            return;
        }
        System.out.println("Input the number of possible symbols in the code:");

        try {
            keyLength = scanner.nextInt();
        } catch (InputMismatchException e) {
            System.out.print("Error: \" \" isn't a valid number.");
            return;
        }
        if (numLength > keyLength) {
            System.out.println("Error: it's not possible to generate a code " +
                    "with a length of 6 with 5 unique symbols.");
            return;
        } else if (keyLength > 36) {
            System.out.println("Error: maximum number of possible symbols in " +
                    "the code is 36 (0-9, a-z).");
            return;
        }
        do {
            random2 = realNum(numLength,keyLength);
        }while (random2 == null);
        while (true) {
            System.out.println("Turn " + count + ":");
            if (givenNumber(random2)) {
                System.out.println("Congratulations! You guessed the secret code.");
                break;
            }
            count ++;
        }
    }

    public static String realNum (int numLength,int keyLength) {
        String KeyString = "0123456789abcdefghijklmnopqrstuvwxyz";
        String keyString = KeyString.substring(0,keyLength);
        boolean check = true;
        String randomNum = null;
        if (numLength < 10) {
            while (check) {
                //long guessNum = System.nanoTime();
                randomNum = getRandomString(keyString,numLength);
                char[] random1 = randomNum.toCharArray();
                int i = 0;
                int j;
                int check1 = 0;
                while (i < numLength - 1) {
                    int numericValue = Character.getNumericValue(random1[i]);
                    j = i + 1;
                    i ++;
                    while (j < numLength) {
                        int numericValue1 = Character.getNumericValue(random1[j]);
                        if (numericValue == numericValue1) {
                            check1 = 1;
                        }
                        j ++;
                    }
                }
                if ((check1 == 0)){
                    check = false;
                }
            }
            char[] starNum = new char[numLength];
            for (int i = 0; i < numLength; i ++) {
                starNum[i] = '*';
            }
            if (keyLength > 11) {
                System.out.println("The secret is prepared: " +
                        String.valueOf(starNum) + " (0-9, a-" + keyString.charAt(keyLength - 1) +").");
            } else if (keyLength == 10) {
                System.out.println("The secret is prepared: " +
                        String.valueOf(starNum) + " (0-9, a).");
            } else {
                System.out.println("The secret is prepared: " +
                        String.valueOf(starNum) + " (0-" + (keyLength - 1) + ").");
            }
            System.out.println("Okay, let's start a game!");
            return randomNum;
        } else {
            System.out.println("error");
            return null;
        }
    }

    //获取指定位数的随机字符串(包含小写字母、大写字母、数字,0<length)
    public static String getRandomString(String KeyString,int length) {
        //随机字符串的随机字符库
        StringBuilder sb = new StringBuilder();
        int len = KeyString.length();
        for (int i = 0; i < length; i++) {
            sb.append(KeyString.charAt((int) Math.round(Math.random() * (len - 1))));
        }
        return sb.toString();
    }

    public static boolean givenNumber (String rN) {
        Scanner scanner = new Scanner(System.in);
        String b = scanner.nextLine();
        char[] aA = rN.toCharArray();
        char[] bB = b.toCharArray();
        int countCow = 0;
        int countBull = 0;
        for (int j = 0; j < rN.length(); j++) {
            if (aA[j] == bB[j]) {
                countBull++;
            } else {
                for (int i = 0; i < rN.length(); i++) {
                    if (aA[j] == bB[i]) {
                        countCow++;
                    }
                }
            }
        }
        if ((countBull != 0) && (countCow != 0)) {
            if ((countBull == 1) && (countCow == 1)) {
                System.out.println("Grade: " + countBull + " bull and "
                        + countCow + " cow");
            } else if (countBull == 1){
                System.out.println("Grade: " + countBull + " bull and "
                        + countCow + " cows");
            } else if (countCow == 1) {
                System.out.println("Grade: " + countBull + " bulls and "
                        + countCow + " cow");
            } else {
                System.out.println("Grade: " + countBull + " bulls and "
                        + countCow + " cows");
            }
        } else if (countBull != 0) {
            if (countBull == 1) {
                System.out.println("Grade: " + countBull + " bull");
            } else {
                System.out.println("Grade: " + countBull + " bulls");
            }
        } else if (countCow != 0) {
            if (countCow == 1) {
                System.out.println("Grade: " + countCow + " cow");
            } else {
                System.out.println("Grade: " + countCow + " cows");
            }
        } else {
            System.out.println("Grade: None.");
        }
        boolean check = false;
        if (countBull == rN.length()) {
            check = true;
        }
        return check;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NIHILISTE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值