猜字母游戏java源代码

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class GuseeLetter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int level;
        System.out.println("GuessintGame>欢迎尝试猜字母游戏!");
        do {
            System.out.print("GuessintGame>请输入游戏级别(5,7,9)?");

            level = scanner.nextInt();
            if (level == 5 || level == 7 || level == 9) { // 选择游戏级别
                break;
            }
        } while (true);

        // int level = 5;
        char[] ans = answer(level); // 生成n个元素的字符串
        System.out.println("GuessintGame>游戏开始,请输入你所猜的"+level+"个字母序列:(exit-退出)");

        String input;
        int count = 0;
        Scanner scan = new Scanner(System.in);
        int score = level * 100; // 每一个字母一百分
        do {
            input = scan.nextLine().trim().toUpperCase();// 接受输入的字符串
            char[] inputArr = input.toCharArray(); // 将字符串转换为字符数组
            int[] check = compare(inputArr, ans);
            count++; // 计算次数
            int total = score - count * 10; // 计算总分
            
            if (input.equals("EXIT")) {
                System.out.println("GuessintGame>谢谢你的尝试,再见!");
                //System.out.println("GuessintGame>正确答案是:" + Arrays.toString(ans));
                break;
            }
            // System.out.println(Arrays.toString(ans));
            if (check[0] == level && check[1] == level) {
                System.out.println("GuessintGame>恭喜你猜对了!你的得分是" + total);
                break;
            }
            System.out.println("GuessintGame>猜对了" + check[0] + "个字符,其中"
                    + check[1] + "个位置正确!(总次数=" + count + ",exit-退出)");
        } while (true);
    }

    
    public static char[] answer(int n) {
        char[] arr = new char[n];
        Random random = new Random();
        char[] allLetters = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G',
                'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
                'T', 'U', 'V', 'W', 'X', 'Y', 'Z', };
        boolean[] flag = new boolean[allLetters.length]; // 默认false
        int j = 0;
        do {
            int num = random.nextInt(allLetters.length);
            if (flag[num]) {
                continue;
            }
            arr[j++] = allLetters[num];
            flag[num] = true;
        } while (j != n);
        return arr;
    }

    
    public static int[] compare(char[] a, char[] b) {
        int[] arr = new int[2];

        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < b.length; j++) {
                if (a[i] == b[j]) {
                    arr[0]++;
                    if (i == j) {
                        arr[1]++;
                    }
                    break;
                } else {
                    continue;
                }
            }
        }
        return arr;
    }
}

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值