猜字母游戏!练练手!大家来找找茬!

http://img.imooc.com/5758df0200013f6706770442.jpg

http://img.imooc.com/5758df110001918106770714.jpg

http://img.imooc.com/5758df1f00010a2706770714.jpg

package com.jc1;

import java.util.*;

public class GuessGame2 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("====================欢迎来到猜字母游戏====================");
		System.out.println("游戏规则:\n" 
				+ "1、游戏中的字符种类可自定义选择(字母和数字),并且区分大小写;\n" 
				+ "2、输入字符的位置与随机生成的字符位置一一对应才算正确;\n"
				+ "3、游戏总分1000分,每猜1次扣除10分,分数为0则退出游戏;\n" 
				+ "4、游戏中字符长度可自定义选择;\n" 
				+ "5、猜字符过程中输入“==”退出游戏。\n\n"
				+ "------------------游戏开始!Ready!Go!------------------");
		int score = 1000; // 总分
		int chs = 0, loc = 0; // 声明猜对字符的个数及位置正确的字符个数
		int choice, count;
		char[] answer = null;
		do {
			System.out.println("请选择猜测的字符种类:\n" 
					+ "1.数字    2.小写字母    3.大写字母    4.大小写字母组合    5.大小写字母与数字组合");
			choice = scan.nextInt();
			if (choice < 1 || choice > 5) {
				System.out.println("你的输入有误!请重新输入!");
			}
		} while (choice < 1 || choice > 5);
		System.out.println("请自定义要猜测的字符长度:(数字1~10,字母1~26)");
		count = scan.nextInt(); // 自定义字符的个数
		scan.nextLine(); // 将nextInt()丢弃掉的回车符(用户上一次输入完成后的回车符)接收,以保证后续nextLine()能正常接收输入
		// 调用生成随机字符方法(共有五种随机方法)
		if (choice == 1) {
			answer = generate1(count);
		} else if (choice == 2) {
			answer = generate2(count);
		} else if (choice == 3) {
			answer = generate3(count);
		} else if (choice == 4) {
			answer = generate4(count);
		} else {
			answer = generate5(count);
		}
		//System.out.println(answer); // 输出系统随机生成的字符
		do {
			System.out.println("请输入" + count + "个字符:");
			String s = scan.nextLine();
			// String s = scan.next().trim().toUpperCase(); //
			// 接收用户输入的字母并自动将小写转换成大写(即不区分大小写)
			// 检查用户输入==退出游戏
			if (s.equals("==")) {
				System.out.println("------------------退出游戏!欢迎继续挑战!------------------");
				break;
			}
			if (s.length() != count) {
				System.out.println("你的输入有误!字符长度必须是" + count + "位!请重新输入!");
				continue;
			}
			char[] input = s.toCharArray();
			int[] var = check(answer, input); // 调用比较方法返回有chs与loc信息的数组check
			chs = var[0];
			loc = var[1];
			System.out.println("你猜对字符个数:" + chs + ",其中位置正确的字符个数:" + loc);
			score -= 10;
			if (score == 0) {
				System.out.println("你太差劲了!IQ有待提升!");
				break;
			}
			if (chs == count && loc == count) {
				rank(count, score); // 调用rank方法输出段位
			}
		} while (chs != count || loc != count);
		scan.close(); // 关闭输出

	}

	// 效率非常高且常用的生成随机字符方法一(纯数字)
	public static char[] generate1(int n) {
		// 方法一:将26个大写字母放到一个数组里面,然后随机生成这个数组的索引(下标),通过索引得到随机字母
		// 方法二:随机生成26个大写字母所对应的int型数字(65~90),再转换成字母即可
		char[] allLetters = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
		boolean[] isRep = new boolean[allLetters.length]; // 创建一个boolean型数组,与allLetters长度一致,所有元素默认为false
		char[] letter = new char[n]; // 创建数组接收随机生成的n个字符
		int temp; // 声名数组索引(下标)
		for (int i = 0; i < letter.length; i++) {
			do {
				temp = new Random().nextInt(allLetters.length); // 生成随机数方法一(allLetters数组下标0~25)
				// temp = (int) (Math.random() * allLetters.length); // 生成随机数方法二
				letter[i] = allLetters[temp]; // 将allLetters数组中下标为temp的元素赋值给letter数组中索引为i的元素
			} while (isRep[temp]);
			isRep[temp] = true; // letter每一次赋值完成后,将与allLetters数组下标对应的isRep数组下标所对应的元素值改为true
		}
		return letter;
	}

	// 效率非常高且常用的生成随机字符方法二(小写字母)
	public static char[] generate2(int n) {
		// 方法一:将26个大写字母放到一个数组里面,然后随机生成这个数组的索引(下标),通过索引得到随机字母
		// 方法二:随机生成26个大写字母所对应的int型数字(65~90),再转换成字母即可
		char[] allLetters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
				's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
		boolean[] isRep = new boolean[allLetters.length]; // 创建一个boolean型数组,与allLetters长度一致,所有元素默认为false
		char[] letter = new char[n]; // 创建数组接收随机生成的n个字符
		int temp; // 声名数组索引(下标)
		for (int i = 0; i < letter.length; i++) {
			do {
				temp = new Random().nextInt(allLetters.length); // 生成随机数方法一(allLetters数组下标0~25)
				// temp = (int) (Math.random() * allLetters.length); // 生成随机数方法二
				letter[i] = allLetters[temp]; // 将allLetters数组中下标为temp的元素赋值给letter数组中索引为i的元素
			} while (isRep[temp]);
			isRep[temp] = true; // letter每一次赋值完成后,将与allLetters数组下标对应的isRep数组下标所对应的元素值改为true
		}
		return letter;
	}

	// 效率非常高且常用的生成随机字符方法三(大写字母)
	public static char[] generate3(int n) {
		// 方法一:将26个大写字母放到一个数组里面,然后随机生成这个数组的索引(下标),通过索引得到随机字母
		// 方法二:随机生成26个大写字母所对应的int型数字(65~90),再转换成字母即可
		char[] allLetters = { '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[] isRep = new boolean[allLetters.length]; // 创建一个boolean型数组,与allLetters长度一致,所有元素默认为false
		char[] letter = new char[n]; // 创建数组接收随机生成的n个字符
		int temp; // 声名数组索引(下标)
		for (int i = 0; i < letter.length; i++) {
			do {
				temp = new Random().nextInt(allLetters.length); // 生成随机数方法一(allLetters数组下标0~25)
				// temp = (int) (Math.random() * allLetters.length); // 生成随机数方法二
				letter[i] = allLetters[temp]; // 将allLetters数组中下标为temp的元素赋值给letter数组中索引为i的元素
			} while (isRep[temp]);
			isRep[temp] = true; // letter每一次赋值完成后,将与allLetters数组下标对应的isRep数组下标所对应的元素值改为true
		}
		return letter;
	}

	// 效率非常高且常用的生成随机字符方法四(大小写字母)
	public static char[] generate4(int n) {
		// 方法一:将26个大写字母放到一个数组里面,然后随机生成这个数组的索引(下标),通过索引得到随机字母
		// 方法二:随机生成26个大写字母所对应的int型数字(65~90),再转换成字母即可
		char[] allLetters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
				's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '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[] isRep = new boolean[allLetters.length]; // 创建一个boolean型数组,与allLetters长度一致,所有元素默认为false
		char[] letter = new char[n]; // 创建数组接收随机生成的n个字符
		int temp; // 声名数组索引(下标)
		for (int i = 0; i < letter.length; i++) {
			do {
				temp = new Random().nextInt(allLetters.length); // 生成随机数方法一(allLetters数组下标0~25)
				// temp = (int) (Math.random() * allLetters.length); // 生成随机数方法二
				letter[i] = allLetters[temp]; // 将allLetters数组中下标为temp的元素赋值给letter数组中索引为i的元素
			} while (isRep[temp]);
			isRep[temp] = true; // letter每一次赋值完成后,将与allLetters数组下标对应的isRep数组下标所对应的元素值改为true
		}
		return letter;
	}

	// 效率非常高且常用的生成随机字符方法五(大小写字母与数字组合)
	public static char[] generate5(int n) {
		// 方法一:将26个大写字母放到一个数组里面,然后随机生成这个数组的索引(下标),通过索引得到随机字母
		// 方法二:随机生成26个大写字母所对应的int型数字(65~90),再转换成字母即可
		char[] allLetters = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
				'i', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '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[] isRep = new boolean[allLetters.length]; // 创建一个boolean型数组,与allLetters长度一致,所有元素默认为false
		char[] letter = new char[n]; // 创建数组接收随机生成的n个字符
		int temp; // 声名数组索引(下标)
		for (int i = 0; i < letter.length; i++) {
			do {
				temp = new Random().nextInt(allLetters.length); // 生成随机数方法一(allLetters数组下标0~25)
				// temp = (int) (Math.random() * allLetters.length); // 生成随机数方法二
				letter[i] = allLetters[temp]; // 将allLetters数组中下标为temp的元素赋值给letter数组中索引为i的元素
			} while (isRep[temp]);
			isRep[temp] = true; // letter每一次赋值完成后,将与allLetters数组下标对应的isRep数组下标所对应的元素值改为true
		}
		return letter;
	}

	// 比较系统随机生成的字符与用户输入的字符
	public static int[] check(char[] answer, char[] input) {
		int m = 0, n = 0;
		for (int i = 0; i < answer.length; i++) {
			for (int j = 0; j < input.length; j++) {
				if (answer[i] == input[j]) {
					m++; // 猜中的字符个数
					if (i == j) {
						n++; // 位置对应的字符个数
					}
					break;
				}
			}
		}
		return new int[] { m, n }; // 将猜中的字符个数与位置对应的字符个数存放到数组中
	}


	public static void rank(int n, int score) {
		if (n >= 4) {
			if (score >= 950) {
				System.out.println("恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========最强王者========");
			} else if (score >= 900) {
				System.out.println("恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========超凡大师========");
			} else if (score >= 850) {
				System.out.println("恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========璀璨钻石========");
			} else if (score >= 800) {
				System.out.println("恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========华贵铂金========");
			} else if (score >= 750) {
				System.out.println("恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========荣耀黄金========");
			} else if (score >= 700) {
				System.out.println("恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========不屈白银========");
			} else if (score >= 600) {
				System.out.println("恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========英勇黄铜========");
			} else {
				System.out.println("恭喜你!猜对了!你的得分:" + score + "分!\n\n======加油吧!骚年!======");
			}
		} else {
			System.out.println("大神!恭喜你!猜对了!你的得分:" + score + "分!\n\n=======这太小儿科了!你需要更大挑战!=======");
		}
	}
}


冰山点水
浏览 2626回答 6
6回答

Genment

挺不错的,但是为什么45行和57行要用到 label a 呢?直接 continue 不可以吗?

奈何MJ

让我想起了我大一的时候

此生不变丶

大神帮忙回答下我这个问题行不http://www.imooc.com/wenda/detail/319256

求学者ph

做的真好 我现在看都看不懂呢
打开App,查看更多内容
随时随地看视频慕课网APP